0

I'm looking for a way to pause a javascript function while waiting for the user to make a choice. I would like it to work like prompt, alert or confirm (they pause the script) but I would like to create my own query box in html.

Is there any way that I can achieve this?

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
hgerdin
  • 637
  • 2
  • 8
  • 24
  • See: http://stackoverflow.com/questions/2035645/when-is-javascript-synchronous – HellaMad Jan 18 '13 at 21:28
  • @MikeChristensen What do you mean by that? – hgerdin Jan 18 '13 at 21:38
  • @user916276 - See the [FAQ](http://stackoverflow.com/faq#howtoask) - Notably: *When you have decided which answer is the most helpful to you, mark it as the accepted answer by clicking on the check box outline to the left of the answer.* – Mike Christensen Jan 18 '13 at 21:50
  • Related: [Sleep in Javascript](http://stackoverflow.com/q/758688/2460971) – numaroth Mar 09 '15 at 20:04

1 Answers1

2

Setup a callback that will be called when the user make the choice then let the javascript end after showing the choices to the user.

Paolo
  • 15,233
  • 27
  • 70
  • 91
  • Thanks a lot! Do you know any good tutorial that shows how to write a good callback function? – hgerdin Jan 18 '13 at 21:39
  • this is an example http://www.w3schools.com/js/tryit.asp?filename=tryjs_events taken from here http://www.w3schools.com/js/js_examples.asp which is a nice site – Paolo Jan 18 '13 at 21:47
  • But does a callback really pause the execution of the javascript code? – hgerdin Jan 18 '13 at 21:48
  • 1
    No, it doesn't. Nothing pauses a JavaScript thread - there's no sleep function. What the callback does is that it waits for a specific action to finish before commencing executing on the code within the callback – kinsho Jan 18 '13 at 21:50
  • 1
    the javascript thread pauses when ends. but the javascript engine keeps running if there are callback that may be triggered by events. – Paolo Jan 18 '13 at 21:52
  • I will look closer into callbacks. Thanks! – hgerdin Jan 18 '13 at 22:16
  • if this seem to you the be right answer (but only in that case!) you may also consider the option to flag it as the right answer. This is how things are supposed to go on Stack O. – Paolo Jan 18 '13 at 22:22