when we use setInterval it delays for specified time and then run the function specified in 1st argument. What should i do if I dont want the delay for the 1st time?
I have one solution that call the function initially and then use setInterval so for the 1st call delay will not be there. but It is not proper solution. So If anybody have some another Solution Then ki开发者_如何转开发ndly let me know that.
setInterval(function(){
doFunction();
}, 15000);
doFunction();
does that imediatelly :)
read this post. Changing the interval of SetInterval while it's running
Peter Bailey has a good generic function, however gnarf also has a nice little example.
You will have to create your own timer for this. SetInterval does not allow you to change the interval delay while its being executed. Using setTimeout within a callback loop should allow you to edit the interval time.
Another way:
(function run(){
// code here
setTimeout(run,1000);
}())
Create a wrapper for that.
function runPeriodically(func, miliseconds) {
func();
return setInterval(func, miliseconds);
}
function onePiece() { /* */ }
runPeriodically(onePiece, 100);
精彩评论