homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

combining setTimeout & setInterval

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



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


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


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


 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?


 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.


 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.


 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.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved