Questions tagged [settimeout]

setTimeout is a global JavaScript method, used to execute a particular function or piece of code after a given delay.

setTimeout is a JavaScript function, which executes a piece of code after a given delay.

The delayed code has to be defined via the first argument. This can be a function reference, or a plain string. Passing a string is not recommended, for the same reasons that the use of eval is discouraged.

The delay is defined in milliseconds. In all browsers except for Internet Explorer 9 and earlier, additional parameters can be added, which are passed to the function.

setTimeout returns a timeout ID, which can be passed to clearTimeout to cancel the execution of the delayed function.

To perform an action repetitively at an interval, use setInterval and clearInterval.

References

5293 questions
49
votes
6 answers

Calling functions with setTimeout()

Simply put... why does setTimeout('playNote('+currentaudio.id+', '+noteTime+')', delay); work perfectly, calling the function after the the specified delay, but setTimeout(playNote(currentaudio.id,noteTime), delay); calls the function playNote all…
quantum rookie
  • 667
  • 1
  • 6
  • 4
49
votes
9 answers

How to tell .hover() to wait?

I have a drop down menu. Now when it's slided down to multiple levels, I'd like it to add wait time for like 2 secs, before it disappears, so the user can get back in, when he breaks the .hover() by mistake. Is it possible? my code for the…
Michal
45
votes
5 answers

What is the reason JavaScript setTimeout is so inaccurate?

I got this code over here: var date = new Date(); setTimeout(function(e) { var currentDate = new Date(); if(currentDate - date >= 1000) { console.log(currentDate, date); console.log(currentDate-date); } else { …
Tomás
  • 3,501
  • 3
  • 21
  • 38
43
votes
6 answers

setInterval/setTimeout return value

Two questions: How is the value returned from setInterval and setTimeout (the ones used to clear the timers) calculated? Is it possible for both the functions to return the same value during runtime? For example: var a = setInterval(fn1, 1000); var…
aditya
  • 1,978
  • 3
  • 17
  • 22
43
votes
3 answers

Checking whether clearInterval has been called?

Given this code: bob = setInterval(function, 1000); clearInterval(bob); Is there now a way to know if that interval has been cleared? Currently, I keep track of this myself, by unsetting 'bob', but I'm curious if my extra line of code is…
swajak
  • 2,669
  • 3
  • 20
  • 22
42
votes
5 answers

What the difference between window.setTimeout() and setTimeout()?

I would like to know what the difference is between window.setTimeout(myFancyFunciton, 1000); and setTimeout(myFancyFunciton, 1000); Both seem to do the exact same thing. When should you use one or the other?
user3073240
  • 563
  • 1
  • 5
  • 9
41
votes
2 answers

How do I do the equivalent of setTimeout + clearTimeout in Dart?

I'm porting some JavaScript to Dart. I have code that uses window.setTimeout to run a callback after a period of time. In some situations, that callback gets canceled via window.clearTimeout. What is the equivalent of this in Dart? I can use new…
Danny Tuppeny
  • 40,147
  • 24
  • 151
  • 275
40
votes
4 answers

Can setTimeout ever return 0 as the id?

I am writing a check to see if a timeout is active. I was thinking of doing this: var a = setTimeout(fn, 10); // ... Other code ... where clearTimeout(a) can be called and set to null if (a != null) { // do soemthing } I was wondering if it…
Aishwar
  • 9,284
  • 10
  • 59
  • 80
39
votes
3 answers

Does Node.js enforce a minimum delay for setTimeout?

In browsers, if you use setTimeout from within a function called by setTimeout then a minimum delay of 4ms will be enforced. Mozilla's developer wiki describes this behaviour, and mentions that it has become standardized in HTML5. Node.js's…
Jeremy
  • 1
  • 85
  • 340
  • 366
38
votes
8 answers

setTimeout runs only once?

function slide() { if($('.current').is(':last-child')){ $('.current').removeClass('.current'); $('#imgholder').first().addClass('.current'); $('#imgholder').animate({left: '3920px'}); } else{ …
pahnin
  • 5,367
  • 12
  • 40
  • 57
38
votes
4 answers

What happens to JavaScript execution (settimeout, etc.) when iPhone/Android goes to sleep?

I have a jQuery Mobile web app which targets iOS and Android devices. A component of the application is a background task, which periodically checks for a.) changes to local data and b.) connectivity to the server. If both are true, the task pushes…
Christopher
  • 1,723
  • 1
  • 18
  • 31
37
votes
3 answers

setTimeout() is not waiting

I am trying to make a seconds countdown with Javascript. Here is my HTML
Please wait 45 seconds before trying…
Mike
  • 23,542
  • 14
  • 76
  • 87
36
votes
9 answers

Is there a function similar to setTimeout() (JavaScript) for PHP?

The question sort of says it all - is there a function which does the same as the JavaScript function setTimeout() for PHP? I've searched php.net, and I can't seem to find any...
Latze
  • 1,843
  • 7
  • 22
  • 29
36
votes
2 answers

How is setTimeout implemented in node.js

I was wondering if anybody knows how setTimeout is implemented in node.js. I believe I have read somewhere that this is not part of V8. I quickly tried to find the implementation, but could not find it in the source(BIG).I for example found this…
Alfred
  • 60,935
  • 33
  • 147
  • 186
35
votes
3 answers

setTimeout ignores timeout? (Fires immediately)

This is my first real dive into JavaScript. Sure I've used it before, but I've never really written anything from scratch. Anyway, I'm having a very strange problem that I hope someone can figure out for me. I'm trying to make the text from a div…
Jonah H.
  • 1,760
  • 4
  • 18
  • 31