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
74
votes
5 answers

Test a function that contains a setTimeout()

I have a close function in my component that contains a setTimeout() in order to give time for the animation to complete. public close() { this.animate = "inactive" setTimeout(() => { this.show = false }, 250) } this.show is…
ed-tester
  • 1,596
  • 4
  • 17
  • 24
68
votes
11 answers

using setTimeout synchronously in JavaScript

I have the following scenario: setTimeout("alert('this alert is timedout and should be the first');", 5000); alert("this should be the second one"); I need the code after the setTimeout to be executed after the code in the setTimeout is executed.…
Nathan
  • 1,865
  • 4
  • 19
  • 25
68
votes
1 answer

What is setTimeout doing when set to 0 milliseconds?

In JavaScript, setTimeout(callback, delay) means "call callback after delay milliseconds". But what if delay is 0? Should it call callback right away? I am confused because of what I see when I run the following code: setTimeout(function() { …
DDan
  • 8,068
  • 5
  • 33
  • 52
67
votes
5 answers

Is calling setTimeout with a negative delay ok?

The following snippet sets a timeout that I'd like to last at least a second: var currentTimeMillis = new Date().getTime(); // do stuff... var sleepTime = 1000 - (new Date().getTime() - currentTimeMillis); Given that sleepTime can be a negative…
ripper234
  • 222,824
  • 274
  • 634
  • 905
64
votes
7 answers

Repeating setTimeout

I am trying to repeat setTimeout every 10 seconds. I know that setTimeout by default only waits and then performs an action one time. How can I repeat the process? setTimeout(function() { setTimeout(function() { console.log("10 seconds"); },…
Daniel
  • 4,202
  • 11
  • 50
  • 68
64
votes
4 answers

How to test a function which has a setTimeout with jasmine?

I need to write a test for a function that has a setTimeout() call inside, but i can't find how i should do. This is the function // Disables all submit buttons after a submit button is pressed. var block_all_submit_and_ajax = function( el ) { …
Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
63
votes
5 answers

Call setTimeout without delay

Quite often see in JavaScript libraries code like this: setTimeout(function() { ... }, 0); I would like to know why use such a wrapper code.
defuz
  • 26,721
  • 10
  • 38
  • 60
62
votes
9 answers

What is the equivalent of javascript setTimeout in Java?

I need to implement a function to run after 60 seconds of clicking a button. Please help, I used the Timer class, but I think that that is not the best way.
60
votes
12 answers

Synchronous delay in code execution

I have a code which needs to be executed after some delay say 5000 ms.Currently I am using setTimeout but it is asynchronous and i want the execution to wait for its return. I have tried using the following: function pauseComp(ms) { var curr…
kavita
  • 739
  • 2
  • 6
  • 7
59
votes
5 answers

How can I disable all setTimeout events?

I am using ajax and asp.net. iI have a javascript function which creates many other javascript functions with setTimeout. After asynchronous postback happenes, I want to disable all of these setTimeouted events. How can I do that?
Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342
57
votes
4 answers

Is setTimeout with no delay the same as executing the function instantly?

I am looking at some existing code in a web application. I saw this: window.setTimeout(function () { ... }) Is this the same as just executing the function content right away?
Aishwar
  • 9,284
  • 10
  • 59
  • 80
55
votes
4 answers

What is minimum millisecond value of setTimeout?

I would like to put var minValue = 0; if ( typeof callback == 'function' ) { setTimeout( callback, minValue ); } this code when I implement callback function with JavaScript. But I've found that modern browsers and some old browsers have…
jwchang
  • 10,584
  • 15
  • 58
  • 89
51
votes
4 answers

JQuery, setTimeout not working

I'm still new to JQuery, on the way to getting my ajax example to work i got stalled with setTimeout. I have broken it down to to where it should add "." to the div every second. The relevant code is in two files. index.html
Bolt_Head
  • 1,453
  • 5
  • 17
  • 26
50
votes
4 answers

setTimeout not working inside forEach

I have a forEach that calls a function. There needs to be a delay between each time it is called. I've put it inside a setTimeout inside the forEach. It isn't respecting the timeout after the first wait. Instead it is waiting once, then running all…
Goose
  • 4,764
  • 5
  • 45
  • 84
50
votes
3 answers

What advantage is there in using the $timeout in AngularJS instead of window.setTimeout?

I had a suggestion to implement a timeout like this: $timeout(function() { // Loadind done here - Show message for 3 more seconds. $timeout(function() { $scope.showMessage = false; }, 3000); }, 2000); }; Can someone tell me…
Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427