0

Is there anyway to do this? I want to create a JavaScript application that can "resume" application from the last checkpoint e.g.

//code.js
var abc = 13;
checkpoint("myLocalStorage");
alert(abc);

the checkpoint function will store every info about the execution such that in the future execution can be resumed right where it was left of as such:

//resume.js
resume("myLocalStorage");

This would be very helpful for executing long script / script with huge loops - I'm not talking about executing some tiny script that preloads images or do some funny animations. I'm talking about using JavaScript as a real number crunching tool where execution can take a long time and demands huge computing power. In these context you can see how useful execution checkpointing could be!

I suppose such thing doesn't exist for JavaScript yet, but if anyone ever comes close to something that remotely resembles it I would still be very grateful.

Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74
lolski
  • 16,231
  • 7
  • 34
  • 49
  • 1
    Out of curiosity, do you know of *any* language that has a feature like this? – Dagg Nabbit Oct 03 '12 at 03:11
  • @GGG: C has setjmp() and longjmp(). See also C# 5 async, or coroutines. – SLaks Oct 03 '12 at 03:16
  • @SLaks for some reason I assumed that the program state was supposed to be saved and this was supposed to work across sessions... maybe I'm reading the question wrong. – Dagg Nabbit Oct 03 '12 at 03:23

3 Answers3

1

In order to make something that "suspend-able" in Javascript, you need to formulate things a little differently than you would in normal program.

Step 1
Decide how much of the problem you are able to do in one pass, for lack of a better word.

Step 2
Store the state in some kind of object. You have no intermediate values, just exactly what is needed to make the next pass

Step 3
Write the code so it can run with a window.setTimeout() function. This makes testing much easier than reloading the page.

In this case, I have a program that converts my whole name to lower case, one step at a time. The only data I need to save is my name, and an index of where along the calculations I am.

Example 1: Uses setTimeout()

<html>
  <head>
    <title>Test Thingy</title>
  </head>
  <body>
  <script>
    var data = {
      name: ["Jeremy", "J", "Starcher"],
      idx: 0
    }

    function doPass() {
      // If at the end of the list
      if (data.idx >= data.name.length) {
        alert("All iterations done:" + data.name.join(" "));
        return;
      }

      // Do our calculation here
      var s = data.name[data.idx];
      s = s.toLowerCase();
      data.name[data.idx] = s;
      data.idx++;
      window.setTimeout(doPass);
    }
    doPass();
  </script>

  </body>
</html>

Example 2: Uses localStorage. Hit 'reload' 4 times to test

<html>
  <head>
    <title>Test Thingy</title>
  </head>
  <body>
  <script>      
    var data;
    data = localStorage.getItem("data");    
    if (data) {
      data = JSON.parse(data);
    } else {
      data = {
        name: ["Jeremy", "J", "Starcher"],
        idx: 0
      }
    }

    function doPass() {
      // If at the end of the list
      if (data.idx >= data.name.length) {
        alert("All iterations done:" + data.name.join(" "));
        return;
      }

      // Do our calculation here
      var s = data.name[data.idx];
      alert(s);
      s = s.toLowerCase();
      data.name[data.idx] = s;
      data.idx++;
      localStorage.setItem("data", JSON.stringify(data));
    }

    doPass();
  </script>

  </body>
</html>
Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74
  • thank you. I thought of your second approach, which is definitely a good approach but I'm thinking something a little more transparent from us the users of the platform. If you consider the possibility of a call to other function(s) that has side-effects I think it could be very hard to keep track of all the execution states – lolski Oct 03 '12 at 10:45
  • @GaneshwaraHerawanHanandaPut -- Oh, I understand exactly how hard it is to keep track of the execution state, which is why knowing where to split things up is critical. Any iterative problem would need to process one *pass* or *iteration* per re-load, rather than trying to only do part of an iteration. I can't imagine you are going to get a simpler solution. – Jeremy J Starcher Oct 03 '12 at 10:53
  • Yes, seems like these are the best solutions after all. Thanks. Regards, Ganesh – lolski Oct 03 '12 at 11:04
0

Javascript is not designed for "[storage on] real number crunching tool where execution can take a long time and demands huge computing power". Here's the best you'll get: http://www.w3schools.com/html/html5_webstorage.asp

Christopher Bales
  • 1,069
  • 1
  • 13
  • 23
0

The latest browsers support yield. We can look into it.

https://developer.mozilla.org/en-US/docs/JavaScript/New_in_JavaScript/1.7

Kernel James
  • 3,752
  • 25
  • 32
  • 1
    can you enlighten me how this could help? I know yield from my little experience with Scala, and this feature is useful for generating the elements of a collection. But I fail to see how this could help solving my problem. Regards, Ganesh – lolski Oct 03 '12 at 10:34
  • @GaneshwaraHerawanHanandaPut The Scala `yield` is something totally different. In the context of this answer, `yield` means that a thread of execution temporarily gives up its execution, so different things can be run. This voluntary yielding is in contrast to pre-emptive multi-tasking. – ziggystar Oct 05 '12 at 08:02