目前我是这样设置的,
var elements = $('#one, $two, #three, #four, #six');
var _temp = 0;
elements.hide(0, function(){
/* by default, the callback will be performed to
every single element in stack */
_temp++; // increment temporary cache
// perform actual callback only on last in list
if (_temp == elements.length) {
// do stuff
}
});
但感觉不对,因为如果我想对下面的另一个回调 241 行执行相同的操作,我必须重置 _temp
并且全局变量太乱了。
我怎样才能简化这个?
最佳答案
一种可能的方法是使用闭包:
var elements = $('#one, $two, #three, #four, #six');
elements.hide(0, (function(){
var _temp = 0;
return function(){
_temp++; // increment temporary cache
// perform actual callback only on last in list
if (_temp == elements.length) {
// do stuff
}
};
})());
如果您想更频繁地使用此模式,您可以创建一个返回回调的函数。
另请注意 .hide()
有一个持续时间作为第一个参数。
关于javascript - 仅在堆栈中的最后一个执行回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11536670/