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
21
votes
4 answers

Google Maps zoomOut-Pan-zoomIn animation

I'm wondering how I get a smooth zoom in animation with the Google Maps API. I have 2 points, one in, let say China, and one in France. When I'm zoomed in on China, and click the button France. I want it to gradually zoom out smooth, one zoom level…
Sindre Sorhus
  • 62,972
  • 39
  • 168
  • 232
21
votes
1 answer

Will a recursive 'setTimeout' function call eventually kill the JS Engine?

Let's say I have some data that I need to get from the server about every 10 seconds. I would have a function that gets the data via AJAX and then call setTimeout to call this function again: function GetData(){ $.ajax({ url:…
Robert Deml
  • 12,390
  • 20
  • 65
  • 92
21
votes
6 answers

type for useRef if used with setInterval, react-typescript

I am doing a simple animation in a next.js app. let flipInterval = useRef(); const startAnimation = () => { flipInterval.current = setInterval(() => { setIsFlipping((prevFlipping) => !prevFlipping); }, 10000); }; for…
Yilmaz
  • 35,338
  • 10
  • 157
  • 202
21
votes
7 answers

Angular 4 setTimeout does not wait

I am creating an angular 4 app with typescript. I'm having a function that needs to be executed every 10 seconds untill a specified stopcondition. I created a loop with some testcode using setTimeout to see if it would work. My Testcode: public…
fangio
  • 1,746
  • 5
  • 28
  • 52
20
votes
0 answers

setTimeout with zero delay used often in web pages, why?

Possible Duplicate: Why is setTimeout(fn, 0) sometimes useful? I noticed a trend in web pages, that they use setTimeout(function(){...}, 0) more often instead of just making the call. I wonder why, couldn't find something about it. Except for the…
Zaibot
  • 226
  • 2
  • 7
20
votes
8 answers

setTimeout() method inside a while loop

I have read the relevant pages on w3schools and other similar questions here but cannot seem to understand what's wrong about the following bit : var myfunc03 = function (i) { document.getElementById('d01').innerHTML += 100-i+"
"; }; var…
havz
  • 303
  • 1
  • 2
  • 5
20
votes
12 answers

setInterval By the minute On the minute

To the javascript enthusiasts, how would you program a setTimeOut (or setInterval) handle to fire by the minute on the minute. So for example, if it is the 51 second of the current time, then fire it in 9 seconds, if it is the 14th second then fire…
bushman
  • 647
  • 2
  • 9
  • 14
20
votes
3 answers

setTimeout or setInterval or requestAnimationFrame

For HTML5 games,with canvas animation for mobile devices. I'm facing some performance issues which differ the speed between each device and the others. requestAnimationFrame speed the animation of the game according to the device speed. setInterval…
Soliman
  • 1,132
  • 3
  • 12
  • 32
19
votes
5 answers

.delay() and .setTimeout()

According to jQuery document on .delay(), The .delay() method is best for delaying between queued jQuery effects. Because it is limited—it doesn't, for example, offer a way to cancel the delay—.delay() is not a replacement for JavaScript's…
Randomblue
  • 112,777
  • 145
  • 353
  • 547
19
votes
4 answers

JavaScript - When exactly does the call stack become "empty"?

I've read several posts/SO threads on event loop, and according to MDN's article, When the stack is empty, a message is taken out of the queue and processed. As a JS novice, what I'm still confused about is -- when exactly does the call stack…
Yibo Yang
  • 2,353
  • 4
  • 27
  • 40
19
votes
5 answers

JavaScript getTimeout?

The window.setTimeout (and related setInterval) function in Javascript allows you to schedule a function to be executed sometime in the future: id = setTimeout(function, delay); where "delay" is the number of milliseconds into the future at which…
nibot
  • 14,428
  • 8
  • 54
  • 58
18
votes
5 answers

Useless setTimeout call (missing quotes around argument?)

I have this sniplet of code in jQuery $element.parent().children().last().hide().show('slide', {direction : 'left'}, 700, function () { $element.delay(2000, function() { $element.parent().children().last().hide('slide', {direction:…
kosta5
  • 1,129
  • 3
  • 14
  • 36
18
votes
5 answers

Does the browser keep track of active timer IDs?

Does the browser keep track of active setInterval and setTimeout IDs? Or is this solely up to the developer to keep track of? If it does keep track of them, is it accessible via the BOM?
ground5hark
  • 4,464
  • 8
  • 30
  • 35
18
votes
2 answers

setTimeOut is Uncaught ReferenceError: setTimeOut is not defined

First, I am new in javascript and I got a problem in setTimeOut ... This is my script code........ $('#nav ul li a').hover(function(){ $(this).next("div").slideDown("fast").siblings("div").slideUp("slow"); …
Aung Thet
  • 3,051
  • 3
  • 13
  • 18
18
votes
9 answers

setTimeout inside for loop

I want a string to appear character-for-character with the following code: function initText() { var textScroller = document.getElementById('textScroller'); var text = 'Hello how are you?'; for(c = 0; c < text.length; c++) { …
richard
  • 14,050
  • 8
  • 37
  • 39