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
24
votes
10 answers

Winforms equivalent of javascript setTimeout

Is there a simple solution/idea/strategy to create a setTimeout equivalent function in a WinForms app. I'm primarily a web developer but am not sure how I'd go about this in a WinForms App. Basically, I have a textbox, and after each keystroke I…
davidsleeps
  • 9,393
  • 11
  • 59
  • 73
24
votes
4 answers

stopping a timeout in reactjs?

Is there a way I can kill/(get rid of) a timeout in reactjs? setTimeout(function() { //do something }.bind(this), 3000); Upon some sort of click or action, I want to be able to completely stop and end the timeout. Is there a way to do this? thanks.
BlueElixir
  • 3,299
  • 8
  • 20
  • 23
23
votes
7 answers

setTimeout Internet Explorer

I have the following javascript in MSIE: setTimeout(myFunction, 1000, param ); this seems to work in all browsers except internet explorer. the param just doesnt get forwarded to the function. looking at the debugger, it is undefined.
clamp
  • 33,000
  • 75
  • 203
  • 299
23
votes
4 answers

Debugging: ESLint Warning "Function declared in a loop contains unsafe references to variable(s)...no-loop-func"

Building a Sort-Visualizer in React using the Create-React-App [https://roy-05.github.io/sort-visualizer/ ] I'm animating each iteration of the loop using setTimeouts. On dev console I get the following warning: Line 156:32: Function declared in a…
roy05
  • 381
  • 1
  • 2
  • 11
23
votes
3 answers

Better way to clearTimeout in componentWillUnmount

I have a working Loading Component that cancels out when it has been loading for 8 seconds. This code works but it feels off to me and I'm wondering if there is a better way to do this. Without setting this.mounted I get the error: Warning: Can…
Turnipdabeets
  • 5,815
  • 8
  • 40
  • 63
23
votes
2 answers

Is using async in setTimeout valid?

I had a asynchronous function in Javascript and I added setTimeout to it. The code looks like that: let timer; clearTimeout(timer); timer =setTimeout(() => { (async() => { await this._doSomething(); …
Marta
  • 2,857
  • 13
  • 46
  • 67
22
votes
4 answers

jQuery delay() - how to stop it?

I already tried stop(true,true), stop(true) and clearQueue(); but this doesn't work. I have problem with fast changing slides, i already have some function what have to reset everything, but it doesn't work. function reset(){ …
rojikada
  • 347
  • 1
  • 3
  • 9
22
votes
4 answers

Force setTimeout to fire its payload earlier than originally set

I have developed a small bit of presentation software which consists of slides and assets for each slide. When a slide is rendered all of its assets are looped through and rendered after a delay using the setTimeout method. looks sweet... Yaaay!,…
lgados
  • 445
  • 4
  • 8
22
votes
6 answers

Recursive setTimeout with async/await

I'm running a setTimeout, recursively, so I only want to call the API one time per minute. Here is my code: ;(async function ticker (minutes) { try { const res = await coingecko.global() const { market_cap_percentage } = res.data.data …
André Alçada Padez
  • 10,987
  • 24
  • 67
  • 120
22
votes
2 answers

Is there ever a good reason to pass a string to setTimeout?

We all know that passing a string to setTimeout (or setInterval) is evil, because it is run in the global scope, has performance issues, is potentially insecure if you're injecting any parameters, etc. So doing this is definitely…
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
22
votes
1 answer

Angular 6 setTimeout Module not found: Error: Can't resolve 'timers'

I have a problem. I want to use setTimeout in angular to call a function after 2 second but I get this: ERROR: Module not found: Error: Can't resolve 'timers'; this is my function: login(user) { console.log(user.value); …
Ciprian
  • 353
  • 1
  • 2
  • 6
22
votes
6 answers

JavaScript setTimeout and changes to system time cause problems

I've noticed that if I set the setTimeout for 1 minute in the future, and then change my system time to 5 minutes in the past, the setTimeout function will trigger in 6 minutes. I did this is because I wanted to see what happens during a daylight…
Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
22
votes
4 answers

Underscore debounce vs vanilla Javascript setTimeout

I understand that debounce in Undercore.js returns a function that will postpone its execution until the wait time is over. My question is, is there an advantage of using debounce over the regular setTimeout function in vanilla Javascript? Don't…
nikjohn
  • 20,026
  • 14
  • 50
  • 86
22
votes
1 answer

Setting a timer in Node.js

I need to run code in Node.js every 24 hours. I came across a function called setTimeout. Below is my code snippet var et = require('elementtree'); var XML = et.XML; var ElementTree = et.ElementTree; var element = et.Element; var subElement =…
Amanda G
  • 1,931
  • 10
  • 33
  • 43
21
votes
7 answers

setTimeout delay doesn't wait for the timeout

I trying to wrap my head around setTimeout, but I can't get it to work properly. I have set up an example here: http://jsfiddle.net/timkl/Fca2n/ I want a text to countdown after an anchor is clicked - but my setTimeout seems to fire at the same…
timkl
  • 3,299
  • 12
  • 57
  • 71