Welcome to WebmasterWorld Guest from 54.166.37.177

Forum Moderators: open

Message Too Old, No Replies

combining setTimeout & setInterval

     
2:28 pm on Dec 5, 2011 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 10, 2005
posts: 387
votes: 0


I thought this would be simple, but I can't find it anywhere on the web; I have a function that uses setInterval to run periodically on the page. I want to delay the start of that function using setTimeout.

The current code is:

var Timer = window.setInterval( "function()", 100 );

I've tried the following, but neither work:

var Timer = window.setTimeout("setInterval( 'function()', 100 )", 1000);
var Timer = setTimeout("window.setInterval( 'function()', 100 )", 1000);
3:14 pm on Dec 5, 2011 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts:4988
votes: 12


First, you should not be using a quoted string as the first value to setTimeout or setInterval. Instead, just use a function reference. It's more efficient.

var Timer = setTimeout(function() {
Timer = setInterval(function () {}, 100);
}, 1000);


Note, if you don't need to cancel the first call to setTimeout, then I wouldn't even bother assigning the return value to Timer, and would instead do something like this:


var Timer;
setTimeout(function() {
Timer = setInterval(function () {}, 100);
}, 1000);
3:47 pm on Dec 5, 2011 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 10, 2005
posts: 387
votes: 0


Thanks Fotiman; in simplifying my code for posting, I neglected to mention that 'function()' is a named function, which I assume explains why it didn't work when I tried it w/o the quotes. That may also explain why I can't get your code to work as written. I've tried various tweaks to your second version:

setTimeout(function() { Timer = setInterval("myfunction() {}", 100); }, 1000);
setTimeout(function() { Timer = setInterval("myfunction()", 100); }, 1000);
4:44 pm on Dec 5, 2011 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 4988
votes: 12


Again, don't use quoted strings! Use the function references.

function myfunction () {
// ...
}
setTimeout(function() { Timer = setInterval(myfunction, 100); }, 1000);
5:10 pm on Dec 5, 2011 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 10, 2005
posts: 387
votes: 0


Thanks -- I didn't expect that to work since it failed before w/o the quotes, but it did. The problem before was that I had parens after the function name, which I thought was standard. Why is this different?
5:28 pm on Dec 5, 2011 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 4988
votes: 12


Putting parens after the function name would cause the function to execute immediately. So the reference that you'd be passing would be whatever was returned from the function call. For example:

function myfunction() {
return 1;
}
setTimeout(myfunction(), 100);

In that example, myfunction would execute when the setTimeout line was encountered in the code, and you would end up with the equivalent of this:

setTimeout(1, 100);

Which makes no sense.
When you pass the function name only, you're passing that function as a reference. Alternatively, you could use an anonymous function definition:

function () {}

With that syntax, the parens are not the ones used to execute the function, but rather the ones that surround the arguments in a function definition.

There are times when you may want a function to execute when setTimeout is called which would return another function reference. For example

function myfunction() {
var myPrivateVar = 1;
return (function () {
// do something with myPrivateVar
});
}
setTimeout(myfunction(), 100);

In this case, myfunction will execute when setTimeout is evaluated, and the result will be:

setTimeout(function () {
// do something with myPrivateVar
}, 100);

In this example, a closure has been created (allowing myPrivateVar to be used), and myfunction() returned another function.
6:04 pm on Dec 5, 2011 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 10, 2005
posts: 387
votes: 0


Wow -- thanks for taking the time to explain that; I had no idea there were that many ways to call a function.
6:29 pm on Dec 5, 2011 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 4988
votes: 12


No problem :)
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members