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
1007
votes
30 answers

How can I pass a parameter to a setTimeout() callback?

I have some JavaScript code that looks like: function statechangedPostQuestion() { //alert("statechangedPostQuestion"); if (xmlhttp.readyState==4) { var topicId = xmlhttp.responseText; setTimeout("postinsql(topicId)",4000); …
Zeeshan Rang
  • 19,375
  • 28
  • 72
  • 100
681
votes
19 answers

Combination of async function + await + setTimeout

I am trying to use the new async features and I hope solving my problem will help others in the future. This is my code which is working: async function asyncGenerator() { // other code while (goOn) { // other code var fileList…
JShinigami
  • 7,317
  • 3
  • 14
  • 22
357
votes
12 answers

TypeScript - use correct version of setTimeout (node vs window)

I am working on upgrading some old TypeScript code to use the latest compiler version, and I'm having trouble with a call to setTimeout. The code expects to call the browser's setTimeout function which returns a number: setTimeout(handler:…
Kevin Tighe
  • 20,181
  • 4
  • 35
  • 36
315
votes
6 answers

Pass correct "this" context to setTimeout callback?

How do I pass context into setTimeout? I want to call this.tip.destroy() if this.options.destroyOnHide after 1000 ms. How can I do that? if (this.options.destroyOnHide) { setTimeout(function() { this.tip.destroy() }, 1000); } When I try the…
JamesBrownIsDead
  • 4,655
  • 6
  • 30
  • 29
245
votes
7 answers

Difference between setTimeout with and without quotes and parentheses

I am learning JavaScript and I have learned recently about JavaScript timing events. When I learned about setTimeout at W3Schools, I noticed a strange figure which I didn’t run into before. They are using double quotes and then call the…
user1316123
  • 2,519
  • 2
  • 14
  • 7
229
votes
11 answers

React hooks - right way to clear timeouts and intervals

I don't understand why is when I use setTimeout function my react component start to infinite console.log. Everything is working, but PC start to lag as hell. Some people saying that function in timeout changing my state and that rerender component,…
ZiiMakc
  • 31,187
  • 24
  • 65
  • 105
209
votes
6 answers

how to write setTimeout with params by Coffeescript

Please tell me how to write javascript below in coffeescript. setTimeout(function(){ something(param); }, 1000);
tomodian
  • 6,043
  • 4
  • 25
  • 29
204
votes
15 answers

Execute script after specific delay using JavaScript

Is there any JavaScript method similar to the jQuery delay() or wait() (to delay the execution of a script for a specific amount of time)?
Niyaz
  • 53,943
  • 55
  • 151
  • 182
186
votes
8 answers

Chrome: timeouts/interval suspended in background tabs?

I was testing the accuracy of setTimeout using this test. Now I noticed that (as expected) setTimeout is not very accurate but for most appliances not dramatically inaccurate. Now if I run the test in Chrome and let it run in a background tab (so,…
KooiInc
  • 119,216
  • 31
  • 141
  • 177
182
votes
8 answers

Using setTimeout on promise chain

Here i am trying to wrap my head around promises.Here on first request i fetch a set of links.and on next request i fetch the content of first link.But i want to make a delay before returning next promise object.So i use setTimeout on it. But it…
AL-zami
  • 8,902
  • 15
  • 71
  • 130
170
votes
12 answers

What is the equivalent to a JavaScript setInterval/setTimeout in Android/Java?

Can anyone tell me if an equivalent for setInterval/setTimeout exists for Android? Does anybody have any example about how to do it?
karse23
  • 4,055
  • 6
  • 29
  • 33
168
votes
4 answers

How do I clear this setInterval inside a function?

Normally, I’d set the interval to a variable and then clear it like var the_int = setInterval(); clearInterval(the_int); but for my code to work I put it in an anonymous function: function intervalTrigger() { setInterval(function() { if…
Oscar Godson
  • 31,662
  • 41
  • 121
  • 201
164
votes
22 answers

How to start search only when user stops typing?

I need to perform a Search when user stops typing.I know I am supposed to use setTimeout() . But with Reactjs I cant find how it works. Can someone please tell me how to invoke a method (that will handle Search) when the user stops typing for a few…
shinite
  • 1,978
  • 3
  • 12
  • 15
159
votes
8 answers

How to make a promise from setTimeout

This is not a realworld problem, I'm just trying to understand how promises are created. I need to understand how to make a promise for a function that returns nothing, like setTimeout. Suppose I have: function async(callback){ …
laggingreflex
  • 32,948
  • 35
  • 141
  • 196
136
votes
11 answers

setInterval in a React app

I'm still fairly new at React, but I've been grinding along slowly and I've encountered something I'm stuck on. I am trying to build a "timer" component in React, and to be honest I don't know if I'm doing this right (or efficiently). In my code…
Jose
  • 4,880
  • 8
  • 27
  • 49
1
2 3
99 100