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
136
votes
7 answers

Why does setTimeout() "break" for large millisecond delay values?

I came across some unexpected behavior when passing a large millisecond value to setTimeout(). For instance, setTimeout(some_callback, Number.MAX_VALUE); and setTimeout(some_callback, Infinity); both cause some_callback to be run almost…
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
132
votes
8 answers

setTimeout / clearTimeout problems

I try to make a page to go to the startpage after eg. 10sec of inactivity (user not clicking anywhere). I use jQuery for the rest but the set/clear in my test function are pure javascript. In my frustation I ended up with something like this…
Tillebeck
  • 3,493
  • 7
  • 42
  • 63
125
votes
5 answers

Get return value from setTimeout

I just want to get the return value from setTimeout but what I get is a whole text format of the function? function x () { setTimeout(y = function () { return 'done'; }, 1000); return y; } console.log(x());
Vainglory07
  • 5,073
  • 10
  • 43
  • 77
105
votes
14 answers

find the time left in a setTimeout()?

I'm writing some Javascript that interacts with library code that I don't own, and can't (reasonably) change. It creates Javascript timeouts used for showing the next question in a series of time-limited questions. This isn't real code because it…
Just Jake
  • 4,698
  • 4
  • 28
  • 33
102
votes
8 answers

Express.js Response Timeout

PROBLEM I've been looking for request/response timeouts for Express.js but everything seems to be related to the connection rather than the request/response itself. If a request is taking a long time, it should be timed out. Obviously this shouldn't…
Xerri
  • 4,916
  • 6
  • 45
  • 54
101
votes
13 answers

How to make `setInterval` behave more in sync, or how to use `setTimeout` instead?

I am working on a music program that requires multiple JavaScript elements to be in sync with another. I’ve been using setInterval, which works really well initially. However, over time the elements gradually become out of sync which is bad in a…
user3084366
  • 1,103
  • 2
  • 9
  • 12
97
votes
7 answers

What happens to setTimeout when the computer goes to sleep?

In a modern web browser, suppose I do a setTimeout for 10 minutes (at 12:00), and 5 minutes later put the computer to sleep, what should happen when the system wakes up again? What happens if it wakes up before the 10 minutes are up (at 12:09) or…
Sudhir Jonathan
  • 16,998
  • 13
  • 66
  • 90
96
votes
6 answers

Delay jquery hover event?

I would like to delay a hover event in jquery. I am reading from a file when user hovers over a link or label. I don't want this event to occur immediately in case the user is just moving the mouse across the screen. Is there a way to delay the…
Brettski
  • 19,351
  • 15
  • 74
  • 97
93
votes
10 answers

How to stop a setTimeout loop?

I'm trying to build a loading indicator with a image sprite and I came up with this function function setBgPosition() { var c = 0; var numbers = [0, -120, -240, -360, -480, -600, -720]; function run() { …
Gihan Lasita
  • 2,955
  • 13
  • 48
  • 65
93
votes
8 answers

Why is the function executed immediately when I use setTimeout?

I'm trying to write simple code with a setTimeout, but the setTimeout just won't wait the time it's supposed to and the code executes immediately. What am I doing wrong? setTimeout(testfunction(), 2000);
Adler
  • 1,432
  • 3
  • 15
  • 27
90
votes
8 answers

How to add pause between each iteration of jQuery .each()?

I'm grabbing an array of jQuery objects and then via .each() modifying each individual jquery with in the array. In this case I'm updated the class names to trigger a -webkit-transition-property to utilize a css transition. I'd like there to be a…
DA.
  • 39,848
  • 49
  • 150
  • 213
90
votes
11 answers

setTimeout in React Native

I'm trying to load a splash screen for an iOS app built in React Native. I'm trying to accomplish this through class states and then a setTimeout function as follows: class CowtanApp extends Component { constructor(props){ super(props); …
Phil
  • 1,614
  • 3
  • 16
  • 17
90
votes
6 answers

Can I see if a timer is still running?

Simple question here that I can't seem to find an answer for: Once a setTimeout is set, is there any way to see if it's still, well, set? if (!Timer) { Timer = setTimeout(DoThis,60000); } From what I can tell, when you clearTimeout, the…
Andrew
  • 5,095
  • 6
  • 42
  • 48
80
votes
4 answers

Best way to iterate over an array without blocking the UI

I am needing to iterate over some large arrays and store them in backbone collections from an API call. What is the best way to do this without making the loop cause the interface to become unresponsive? The return of the ajax request also blocks as…
georgephillips
  • 3,540
  • 4
  • 23
  • 30
77
votes
8 answers

NodeJS Timeout a Promise if failed to complete in time

How can I timeout a promise after certain amount of time? I know Q has a promise timeout, but I'm using native NodeJS promises and they don't have .timeout function. Am I missing one or its wrapped differently? Alternatively, Is the below…
AlexD
  • 4,062
  • 5
  • 38
  • 65