我使用这个代码包的部分代码中使用它这个样子,请问这个“延迟”功能的工作原理
var delay = (function() {
// SET TIMER
var timer = 0;
// RETURN SET TIMEOUT FUNCTION
return function(callback, ms) {
clearTimeout(timer);
timer = setTimeout(callback, ms);
};
})();
我这样称呼它,
delay(function() {
.......
}, 1000);
它会延迟1000毫秒,但我不明白是怎么回事,谢谢:)
我建议你问一下http://codereview.stackexchange.com/ – Gerep 2012-04-08 22:36:25
看看['clearTimeout'](https://developer.mozilla.org/en/DOM/window.clearTimeout)和[ 'setTimeout'](https://developer.mozilla.org/en/DOM/window.setTimeout)。 – 2012-04-08 22:37:16
你可能也想看看匿名函数的定义。这里是[链接](http://stackoverflow.com/questions/1140089/how-does-an-anonymous-function-in-javascript-work)。 – DiamRem 2012-04-08 22:44:03