1

Ok I have a random dice number generator. and it will have a for loop and inside the loop I am trouble having to figure out how to wait one second like this below.

Loading. wait one sec Loading.. wait one sec ...

I can do the rest I just need some help with this.

CDW
  • 33
  • 5

4 Answers4

3

Use setInterval:

window.setInterval(func, delay[, param1, param2, ...]);

or setTimeout:

setTimeout(function() {  }, 1000);

'setInterval' vs 'setTimeout':

setTimeout(expression, timeout); runs the code/function once after the timeout.

setInterval(expression, timeout); runs the code/function in intervals, with the length of the timeout between them.

Community
  • 1
  • 1
0

See https://developer.mozilla.org/en/docs/Web/API/window.setInterval

Will call the function at a particular interval

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0

In function foo, you can define all stuff that you need to be executed.

var foo = function{
}

var timeout = setInterval(foo, 1000);

and when you want to stop execution

clearInterval(timeout);
Gaurav
  • 1,078
  • 2
  • 8
  • 18
0

You can either use setTimeout or setInterval :

timer = setTimeout(function(){/*your code here */}, 1000);

or

timer = setInterval(function(){/*your code here */},1000);

and once you would like to clear the timer use :

clearTimeout(timer);

or

clearInterval(timer);

Mehdi Karamosly
  • 5,388
  • 2
  • 32
  • 50