homepage Welcome to WebmasterWorld Guest from 54.82.229.76
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
combining setTimeout & setInterval
sssweb




msg:4394406
 2:28 pm on Dec 5, 2011 (gmt 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);

 

Fotiman




msg:4394422
 3:14 pm on Dec 5, 2011 (gmt 0)

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);

sssweb




msg:4394436
 3:47 pm on Dec 5, 2011 (gmt 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);

Fotiman




msg:4394450
 4:44 pm on Dec 5, 2011 (gmt 0)

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

function myfunction () {
// ...
}
setTimeout(function() { Timer = setInterval(myfunction, 100); }, 1000);

sssweb




msg:4394461
 5:10 pm on Dec 5, 2011 (gmt 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?

Fotiman




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

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.

sssweb




msg:4394480
 6:04 pm on Dec 5, 2011 (gmt 0)

Wow -- thanks for taking the time to explain that; I had no idea there were that many ways to call a function.

Fotiman




msg:4394488
 6:29 pm on Dec 5, 2011 (gmt 0)

No problem :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved