[英]setTimeout dynamic delay
I have global setTimeout
function as follow: 我有全局
setTimeout
函数如下:
myTimer = function(){
var timer;
this.set = function(saveFormCB) {
timer = setTimeout(function() {
saveFormCB();
}, 10000)
};
this.clear = function() {
clearInterval(timer);
};
return this;
}();
Above will delay saveFormCB() execution 10 second if I do like below: 如果我喜欢下面,上面将延迟saveFormCB()执行10秒:
myTimer.clear()
myTimer.set(function () {
saveFormCB()
});
Some component need faster execution than 10 second, say 5 second. 某些组件需要比10秒更快的执行速度,比如5秒。 So I need dynamic delay for myTimer function.
所以我需要myTimer函数的动态延迟。 I did below but not working:
我在下面做但没有工作:
myTimer = function(a){
var timer;
this.set = function(saveFormCB) {
timer = setTimeout(function() {
saveFormCB();
}, a || 10000)
};
this.clear = function() {
clearInterval(timer);
};
return this;
}();
Any help would be appreciated 任何帮助,将不胜感激
Thanks 谢谢
ADDITIONAL INFO 附加信息
myTimer
execute on input event. myTimer
在输入事件上执行。 Everytime no typing, myTimer
will be executed and the value will be saved to db (with saveFormCB() function) after 10 second myTimer
was executed 每次没有输入,
myTimer
将被执行,并且值将被保存到db(使用saveFormCB()函数)10秒后执行myTimer
First of all, your iife is being passed window
as this, so your myTimer is a reference to the window object and set and clear are globals. 首先,你的iife正在传递
window
,所以你的myTimer是对窗口对象的引用,set和clear是全局的。 Test it. 测试一下。 If you go into strict mode, you'll get an error.
如果你进入严格模式,你会收到一个错误。
What you need is not to make it an iife and make it a constructor, and make it Timer
just as a matter of convention for constructors 你需要的是不让它的iife并使其成为一个构造器,并使其
Timer
就像约定的对构造的问题
Timer = function(a){
var timer;
this.set = function(saveFormCB) {
timer = setTimeout(function() {
saveFormCB();
}, a || 10000)
};
this.clear = function() {
clearInterval(timer);
};
return this;
};
var myTimer = new Timer(500);
First of all I don't think this code is right. 首先,我不认为这段代码是对的。
myTimer.set(function () { //new function
saveFormCB() // calling another function inside this function.
});
Instead you can just do this 相反,你可以这样做
myTimer.set(saveFormCB); //directly pass the function call as a parameter.
Now coming to making your timer dynamic. 现在来让你的计时器动态。 Use the below code.
使用以下代码。
myTimer = function(){ //remove the parameter here
var timer;
this.set = function(saveFormCB,Timer) { //add the timer parameter here
timer = setTimeout(function() {
saveFormCB();
}, Timer || 10000)
};
this.clear = function() {
clearInterval(timer);
};
return this;
}();
And you can execute it like below, 你可以像下面那样执行它,
myTimer.set(saveFormCB,3000);
Oh, you're not passing a variable into the closure properly. 哦,你没有正确地将变量传递给闭包。
var THERE_SHOULD_BE_A_VALUE_HERE = 5000;
myTimer = function(a){
var timer;
this.set = function(saveFormCB) {
timer = setTimeout(function() {
saveFormCB();
}, a || 10000)
};
this.clear = function() {
clearInterval(timer);
};
return this;
}(THERE_SHOULD_BE_A_VALUE_HERE);
But this is not dynamic which is what you want right? 但这不是动态的,这是你想要的吗? The simple solution would be to add a to the set function in mytimer and then execute it with a second parameter
简单的解决方案是在mytimer中添加一个set函数,然后用第二个参数执行它
myTimer = function(){
var timer;
this.set = function(saveFormCB,a) {
timer = setTimeout(function() {
saveFormCB();
}, a || 10000)
};
this.clear = function() {
clearInterval(timer);
};
return this;
}();
And you'd call it like so: 你会这样称呼它:
myTimer.set(function(){console.log('Hello World')},5000)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.