I have global setTimeout
function as follow:
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:
myTimer.clear()
myTimer.set(function () {
saveFormCB()
});
Some ponent need faster execution than 10 second, say 5 second. So I need dynamic delay for myTimer function. 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. Everytime no typing, myTimer
will be executed and the value will be saved to db (with saveFormCB() function) after 10 second myTimer
was executed
I have global setTimeout
function as follow:
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:
myTimer.clear()
myTimer.set(function () {
saveFormCB()
});
Some ponent need faster execution than 10 second, say 5 second. So I need dynamic delay for myTimer function. 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. Everytime no typing, myTimer
will be executed and the value will be saved to db (with saveFormCB() function) after 10 second myTimer
was executed
-
Shouldn't you
clearTimeout
instead ofclearInterval
? – Nikolay Ermakov Commented Mar 10, 2016 at 2:34 - I don't know. This is for autosave form. clearInterval for clearing the setTimeout but haven't executed and setTimeout for new value – prime Commented Mar 10, 2016 at 2:37
-
Assuming you passed a number through the
a
variable this should work. Please provide more code or examples as to how you would execute these functions. – 8eecf0d2 Commented Mar 10, 2016 at 2:44 - @brod on input event. Everytime no typing, myTimer will be executed – prime Commented Mar 10, 2016 at 3:11
3 Answers
Reset to default 2First 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 ing 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);
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. 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
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);
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 = 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)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745198382a4616203.html
评论列表(0条)