admin管理员组文章数量:1301525
I am using this code to wrap parts of code in it is used like this,
var delay = (function() {
// SET TIMER
var timer = 0;
// RETURN SET TIMEOUT FUNCTION
return function(callback, ms) {
clearTimeout(timer);
timer = setTimeout(callback, ms);
};
})();
I call it like this,
delay(function() {
.......
}, 1000);
And it will delay it by 1000 milliseconds, but I do not understand what is going on thanks :)
I am using this code to wrap parts of code in it is used like this,
var delay = (function() {
// SET TIMER
var timer = 0;
// RETURN SET TIMEOUT FUNCTION
return function(callback, ms) {
clearTimeout(timer);
timer = setTimeout(callback, ms);
};
})();
I call it like this,
delay(function() {
.......
}, 1000);
And it will delay it by 1000 milliseconds, but I do not understand what is going on thanks :)
Share edited Apr 8, 2012 at 22:46 gdoron 150k59 gold badges302 silver badges371 bronze badges asked Apr 8, 2012 at 22:32 cgwebprojectscgwebprojects 3,4826 gold badges31 silver badges40 bronze badges 3- I suggest you ask that on codereview.stackexchange. – user745235 Commented Apr 8, 2012 at 22:36
-
Have a look at
clearTimeout
andsetTimeout
. – Felix Kling Commented Apr 8, 2012 at 22:37 - 1 You may also want to look at anonymous function definition. Here is a link . – DiamRem Commented Apr 8, 2012 at 22:44
3 Answers
Reset to default 7The delay is a function that will return another function. The timer variables is inside the closure of the delay function so it can still be accesed by the returning function. The function. You could also write it like this
var delay;
var timer = 0;
delay = function(callback, ms) {
clearTimeOut(timer);
timer = setTimeout(callback, ms);
}
The problem that you have now is that if you call delay twice it will overwrite the timer variables so the second delay will overwrite the timer variable. I tested this out and it seems that your function is also broken it should be:
var delay = function(){
// SET TIMER
var timer = 0;
// RETURN SET TIMEOUT FUNCTION
return function(callback, ms){
clearTimeout(timer);
timer = setTimeout(callback, ms);
};
};
delay()(function(){console.log("hello1");}, 5000);
delay()(function(){console.log("hello2");}, 5000);
If your code does the same it will only trace hello2 because the first one will overwrite the timer variable.
Unless your intention is that a second delay will stop the first delay you should use a different approuch.
The first thing this code does is to execute this function (thanks to the ()
at the very end of the code you posted):
function() {
// SET TIMER
var timer = 0;
// RETURN SET TIMEOUT FUNCTION
return function(callback, ms) {
clearTimeout(timer);
timer = setTimeout(callback, ms);
};
}
and store the result in delay
. When executed, this function creates a closure with the local variable timer
that severs as a local counter. The function then returns the inner function:
function(callback, ms) {
clearTimeout(timer);
timer = setTimeout(callback, ms);
}
Since it is in closure, this inner function has access to the timer
variable. No other outside code can access timer
. This method is used to allow multiple timers to be running at once without having to worry about multiple timer
variables.
Imagine it like this: delay
now contains a function (function(callback, ms) {...
) which has access to a local variable, timer
. The variable timer
is invisible as far as any outside code is concerned. Only the function that delay
contains can access it.
You then invoke that inner function by calling delay(callback, timeout)
.
in the first piece of code u make a variable that is delay and assign if to the return value of a function.. which is in turn returning a function which is
return function(callback, ms) {
clearTimeout(timer);
timer = setTimeout(callback, ms);
}
so this is actly ur delay varible. nw so when u call it like ..
delay(function() {
.......
}, 1000);
this wud work as expected..
also notice the ()
at the end where u assing the delay
variable.. that in javascript means to run a function as soon as it is encountered.. so when the first part of the code i seen it is run and delay
variable is assigned a function.. which u call in the second piece of code
本文标签: javascriptHow does this quotdelayquot function worksStack Overflow
版权声明:本文标题:javascript - How does this "delay" function works - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741678109a2392004.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论