1

I'm training a neural network in the browser. I am considering calling a function that could run for an extended period (overnight), and I'd like to be able to terminate the function execution in a non-fatal way (so I don't lose my data) once I'm happy.

I'm looking for a general solution that would allow any looping function to terminate in its current state from the console or a ui element with minimal changes.

I've seen this question but mine is a bit different; it's now 2016 and we have webworkers and post message, which seem like they would provide the tools for a solution.

7
  • 1
    ES6 generators or ES7 async will be helpful Commented Oct 6, 2016 at 14:33
  • 1
    do you have any code for what you've tried? Commented Oct 6, 2016 at 14:34
  • 1
    You can press the pause button in the browser debugger sources tab to pause execution. Commented Oct 6, 2016 at 14:37
  • if I under stood it right you want some logic to be triggered and triggering each other with some web frontend..? look at node-red Commented Oct 6, 2016 at 14:59
  • If your function runs for an extended period, I imagine you are either doing huge for-loops or recursive stuff? If so, you can easily realize a pause by checking some global variable before hopping into the next iteration. Your pause action then sets this global variable true, and the recursive/loop function will stop and should save it's already computed data somewhere. Continue would then be to call the same function with the remaining data, and adding the previously computed data to the result. Commented Oct 6, 2016 at 14:59

1 Answer 1

3

Well...if you use recursion the pause thing works. But sadly I cannot recommend it, since the call-stack-size will lead to an error with iterations > 13000.

To demonstrate the possibility, I had to include a setTimeout before the next iteration, because else for 10000 the operation is too fast to pause it in between:

var pause = false;
var pause_iteration;
function recursiveLoop(iteration, maximum) {
    if (iteration === maximum) {
        console.log("Loop finished!");
    } else if (pause) {
        console.log("Loop paused at: " + iteration);
        pause_iteration = iteration;
    } else {
        var M = Math.sqrt(Math.random());
        setTimeout(function () {
            recursiveLoop((iteration + 1), maximum)
        }, 100);
    }
}
recursiveLoop(0, 10000);
// set pause = true at a later point
pause = true;
Sign up to request clarification or add additional context in comments.

1 Comment

That's what you meant. Thanks for the example, and it might help someone who wants less than 10000 iterations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.