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
33
votes
3 answers

setTimeout ReactJS with arrow function es6

I'd like to know how to use setTimeout() on ReactJS, because I'm doing this: timerid = setTimeout( () => this.reqMaq( obj['fkmaqid'] ), 2000 ) and it calls twice the function this.reqMaq(). How do I prevent the first call? and just keep the call…
Rafael Mora
  • 1,095
  • 2
  • 13
  • 21
32
votes
4 answers

How to make a jquery function call after "X" seconds

I have a jquery function and I need to call it after opening the website in an Iframe. I am trying to open a weblink in an Iframe and after opening it I need to call the below function. So how do I do that? Here is my function:
coder
  • 13,002
  • 31
  • 112
  • 214
32
votes
2 answers

difference between setTimeout in javascript and $timeout service in angularjs

Iam new to angular framework.Here is my scenario where, I want to change my $scope.variable after a period of time so i used javascript setTimeout method. $scope.variable = 'Previous'; setTimeout(function(){ $scope.variable='NEXT'; },3000); This…
31
votes
5 answers

Is it possible to chain setTimeout functions in JavaScript?

Is it possible to chain setTimout functions to ensure they run after one another?
xiatica
  • 1,546
  • 2
  • 20
  • 24
30
votes
4 answers

how many javascript setTimeout/ setInterval call can be set simultaneously in one page?

I have to use atleast 2 setTimeouts and 1 setInterval. Does this have any dependency on the browser or javascript engine being used?
rabbit
  • 301
  • 1
  • 3
  • 3
30
votes
6 answers

Wait until setInterval() is done

I would like to add a small dice-rolling effect to my Javascript code. I think a good way is to use the setInterval() method. My idea was the following code (just for testing): function roleDice() { var i = Math.floor((Math.random() * 25) + 5); …
Andre Hofmeister
  • 3,185
  • 11
  • 51
  • 74
29
votes
4 answers

How to cancel timeout inside of Javascript Promise?

I'm toying with promises in JavaScript and tried to promisify setTimeout function: function timeout(ms) { return new Promise(function(resolve, reject) { setTimeout(function() { resolve('timeout done'); }, ms); }); } var…
spirytus
  • 10,726
  • 14
  • 61
  • 75
29
votes
14 answers

JavaScript : For loop with timeout

I want that my for loop should not be executed at once, but wait for timeout after each iteration. For eg : for(var i=0; i<10; i++) { console.log(i); //wait for 1000 } I found many solutions on stack-overflow like this one : for (var…
PG1
  • 1,220
  • 2
  • 12
  • 27
29
votes
5 answers

Are equal timeouts executed in order in Javascript?

Suppose I do setTimeout(foo, 0); ... setTimeout(bar, 0); Can I be sure foo will begin executing before bar? What if instead of 0 I use a timeout of 1, 10, or 100 for bar? Simple experiments show that in the case of equal timeout values the…
jkl
  • 441
  • 4
  • 7
29
votes
1 answer

Backbone.Marionette onRender callback fires before view is rendered in browser?

The setting I have Backbone.Marionette.ItemView which renders some content. When the content is rendered I'd like to apply a jQuery plugin which turns part of the view into a container with a scrollbar. The scrollbar is implemented completely in…
Ruben Vreeken
  • 956
  • 1
  • 13
  • 22
28
votes
4 answers

What is the nodejs setTimeout equivalent in Golang?

I am currently studying, and I miss setTimeout from Nodejs in golang. I haven't read much yet, and I'm wondering if I could implement the same in go like an interval or a loopback. Is there a way that I can write this from node to golang? I heard…
Hokutosei
  • 2,114
  • 5
  • 24
  • 42
27
votes
6 answers

Global variable is logged as undefined when passed as parameter to setTimeout callback function

I have some JS code as below: var x = self.someAJAXResponseJSON; // x has some object value here. setTimeout(function(x){ console.log("In setTimeout:", x); // But x is undefined here }, 1000); So I want to pass x to the setTimeout callback…
copenndthagen
  • 49,230
  • 102
  • 290
  • 442
25
votes
5 answers

How to setTimeout on async await call node

How can I add a setTimeout to my async await function call? I have request = await getProduct(productids[i]); where const getProduct = async productid => { return requestPromise(url + productid); }; I've tried request = await…
jenryb
  • 2,017
  • 12
  • 35
  • 72
25
votes
3 answers

How to use "setTimeout" to invoke object itself

Why can't I use setTimeout in a javascript object? Message = function () { ... ... this.messageFactory = ... this.feedbackTag = document.getElementById('feedbackMessages'); this.addInfo = function (message) { …
jojo
  • 13,583
  • 35
  • 90
  • 123
24
votes
6 answers

Using a setTimeout in a async function

I have a async function that waits for an axios call to complete before proceeding. The problem is that I need to put a timeout on the axios call to half a second so that I don't hit the shopify API call limit. async function…