0

Is it possible to halt javascript execution, until a particular button is pressed, or a timeout perhaps..?

someCalls()
someCode()
...
stopUntilEvent() // could be after time, or in page button is pressed...
...
moreArbitraryCalls()

The only way I can think of doing this now is to use browser's alert or confirm dialogs. Is there any way I can crate my own dialog with similar behavior?

Billy Moon
  • 57,113
  • 24
  • 136
  • 237
  • possible duplicate of [Is it possible to stop javascript execution?](http://stackoverflow.com/questions/9298839/is-it-possible-to-stop-javascript-execution) – Asons Feb 26 '14 at 09:58

1 Answers1

0

I'm not quite sure what you are asking.

If you wanted stop execution of certain code while keeping the page interactive there isn't really an easy way to do that. You could take whatever process you want to halt and then break it into a series of steps that check a flag and then continue executing or wait until the status of the flag changes by occasionally polling the flag via setTimeout. Something like:

<button id="pause">Pause</button>
<script>
var currentStep = 0,
  pauseButton = document.getElementById('pause'),
  pauseSteps = false,
  steps = [
    function () { /* step one */},
    function () { /* step two */},
    function () { /* etc. */}
  ],
  doSteps = function () {
    if (currentStep < steps.length) {
      if (!pauseSteps) {
        steps[currentStep]();
        currentStep += 1;
      } else {
        setTimeout(doSteps, 250);
      }
    }
  };

pauseButton.addEventListener('click', function (e) {
  var newText;
  e.preventDefault();

  newText = pauseSteps ? 'Resume' : 'Pause';
  pauseButton.textContent = newText;
  pauseSteps = !pauseSteps;
}, false);

doSteps();
</script>

If you just want to break in the debugger, you could use the debugger; statement, it will halt execution just like you had manually set a break point in the debugger.

Useless Code
  • 12,123
  • 5
  • 35
  • 40
  • On a side note, many Visual Studio extensions which act on JS will point at the `debugger;` statement and signal it as an error ... it's not. – Alex Mar 03 '14 at 08:39