0
//--Need a way to set a delay here--\\\

  var result = ['Rock', 'Paper', 'Scissors',][Math.floor(Math.random() * 3)]
  cpuReply.value = result
//--end of script--\\\

Okay so I am trying to find a way to add a delay before these two lines of code. I have heard a lot of advice about using the .delay, but I don't know how to define it, and it often says unexpected number if I do this

  delay(5000)
  var result = ['Rock', 'Paper', 'Scissors',][Math.floor(Math.random() * 3)]
  cpuReply.value = result

I am not sure I typed in the delay right (tell me if it is wrong) and please help me figure this out.

2 Answers2

2

window.setTimeout is what you are looking for.

Keep in mind it is async, so other things can (and will) execute outside the timed out function. setTimeout is preferred to other options because it is "non-blocking". IE: it does not cause the webpage to freeze while waiting.

keep in mind: setTimeout should not be used to measure time. This is because when the timeout completes the function gets added to the callstack, so you cannot guaranty it will be executed immediately.

window.setTimeout(function(){ 
    var result = ['Rock', 'Paper', 'Scissors',][Math.floor(Math.random() * 3)]
    cpuReply.value = result

}, 5000);// time is in 1000ths of a second 
Burdock
  • 1,085
  • 1
  • 9
  • 22
  • Preferred to what other options? –  Oct 11 '15 at 22:54
  • `setInterval` mainly. Don't use `setInterval` for anything but prototyping functionality. If you need something to be called regularly just have a function call setTimeout on it's self. – Burdock Oct 12 '15 at 07:52
  • But `setInterval` is also non-blocking. –  Oct 12 '15 at 07:55
  • with `setInterval ` there is no guarantee that the function will actually execute when you ask it to If the function you call on an interval takes too long to complete. In short, some invocations will simply end up being dropped... also it ignores errors – Burdock Oct 12 '15 at 08:02
  • If you need a blocking delay... First you should reconsider the idea, but if it is required you can use `while (Date.now() < startDate + 5000) {} ` – Burdock Oct 12 '15 at 08:08
  • There is also no such guarantee with `setTimeout` either. I do not believe that `setInterval` will drop calls. I do not know what you mean by ignoring errors. If you mean errors in the callback, that also applies to `setTimeout` as well. –  Oct 12 '15 at 08:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/92015/discussion-between-burdock-and-torazaburo). – Burdock Oct 12 '15 at 08:14
  • If an error occurs in part of the code that is called by `setInterval`, it wouldn't break and keeps executing the faulty code. The issues like dropped calls, out of order calls, and such start to crop up when you do any networking inside `setInterval`. – Burdock Oct 12 '15 at 08:26
-1

How about using

setTimeout()

Something like this

setTimeout(function () {
            var result = ['Rock', 'Paper', 'Scissors',][Math.floor(Math.random() * 3)]
            cpuReply.value = result
 }, 5000);
Ronny K
  • 3,641
  • 4
  • 33
  • 43