2015-08-28 86 views
1

我通常做例如为:只有运行自定义功能后,功能完整的jQuery

$(".item").fadeIn(function(){ 
    alert('done'); 
}); 

哪个作品? (我相信是正确的?)但我如何使用自定义函数做到这一点?

E.g.

$(".item").customFunction(function(){ 
    customFunctionTwo(); 
}); 
+0

呼叫在功能'结束的功能函数A(){。 ........... b()}' – Tushar

+0

你在谈论一个jQuery插件吗? – RRK

回答

4

基本上它看起来像这样

$.fn.customFunction = function (callback){ 
    //some code 
    callback(); 
} 
$('.item').customFunction(function() { 
    customFunctionTwo(); 
}); 
0

我想你应该看的承诺https://api.jquery.com/promise/

$.fn.customFunction = function (callback){ 
    var myFn = function(){ 
     /* your code along with settimeout as well if you choose*/ 
     //example 
     return $("div").fadeIn(800).delay(1200).fadeOut(); 
    } 
    $.when(myFn()).done(function() { 
     callback(); 
    }); 
}  

$('.item').customFunction(function() { 
    customFunctionTwo(); 
});