4

I have a long function in JavaScript and I am going to pause it at some points. I know that I can do that using setTimeout(myFunction, 100) and clearTimeout(). But, as I know, after using setTimeout(myFunction, 100) my function would be executed from its first line after 100 milliseconds and thereafter every 100 milliseconds. I am wondering if I can pause my function on its line 50 and then resume it right after this line which is 51. Is that possible?

5
  • 1
    Please provide us some code so that we may be able to provide better answers. Commented Oct 19, 2014 at 3:02
  • You may also want to see web workers, if you are just make calculations. html5rocks.com/en/tutorials/workers/basics Commented Oct 19, 2014 at 3:03
  • 3
    I think function* is what you want. For es5 compatible function, you can mock yield by switch case. Commented Oct 19, 2014 at 3:08
  • Thanks for helping me to state my question in a correct way and for the link as well. I just made a comment under the second answer. your comments there would be appreciated. Commented Oct 19, 2014 at 3:24
  • Nothing can be done here that doesn't itself create additional quirks and enter the realm of asynchronous programming. For instance, the code splitting the function into two parts and calling setTimeout twice will return and keep running. setTimeout only schedules. It does not call or execute anything itself. If other code is written to assume the two parts are finished because the function returned, that becomes a bug. This doesn't make answers wrong. It emphasizes the need to learn how the Javascript event loop works, what asynchronous programming is, why there isn't a sleep() in JS Commented Oct 24, 2014 at 3:53

2 Answers 2

2

Here's one way to do it. Make sure all variables you need always to be scope are declared in the outer function. Due to Javascript scope rules, they will be accessible in each subfunction.

function paused() {
  // Define variables you'll need throughout the function.
  var var1, var2, var3;

  // First part of the function
  function part1() {
    // 50 lines
  }

  // Second part of the function
  function part2() {
    // 50 lines
  }

  setTimeout(part1, 0);
  setTimeout(part2, 100);
}

Supposing there are many parts, the timeouts can even be put into a loop:

function paused() {
  // Define variables you'll need throughout the function.
  var var1, var2, var3;

  // All function parts.
  var parts = [
    function() {
      // 50 lines
    },
    function() {
      // 50 lines
    },
    function() {
      // 50 lines
    }
    // More?
  ];

  for (var i = 0; i < parts.length; i++) {
    setTimeout(parts[i], i * 100);
  }
}

Make sure to be cautious about use of this, since the inner functions will rebind it.

Note that the global function will always execute the paused parts in order, regardless of whether each individual portion takes more than 100 ms, due to how the Javascript event queue works. When the event queue sees that multiple setTimeouts are eligible to be executed at the same time, the one queued first takes priority.

Sign up to request clarification or add additional context in comments.

6 Comments

var function paused is certainly interesting to try to decipher as English, but it's not valid JavaScript. :) (you have an extra var at the beginning of each of your code blocks)
if any part takes more than 100ms to be executed, the global function would break.
He could insert each setTimeout inside the other.
@Deuterium: The Javascript event model will execute timeouts in queue order, and timeouts are queued in the natural order. So this is not a concern. "If in the same call frame two calls are made to setTimeout – passing the same value for a second argument – their callbacks will be queued in the order of invocation."
@FengyangWang ...oh, nice! tanks for teaching! i still thinking in c++.. apologize.
|
0

You're confusing setTimeout and setInterval.

However, that's totally impossible.

Instead, you need to break it into two functions, and call the second one from the first one using setTimeout.

Depending on what you're actually trying to do, promises will probably help.

9 Comments

How is the OP confusing setTimeout and setInterval? What the OP wants to do is not impossible. You could use generators to suspend a function midway. I don't see how promises could possibly help. This answer doesn't add anything valuable. It should be a comment instead.
The OP used the setinterval tag. That could perhaps be corrected.
@Aadit M Shah - Well if you read it again, you will see that he is confusing setTimeout and setInterval.
@ThanasisGrammatopoulos No, he is not. If you call a setTimout(foo, 100) within the function foo then you are calling setTimeout recursively which means that foo will be called every 100 milliseconds. That's definitely not the same as setInterval. However, the OP should have clarified with some code.
It seems you are right... :/
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.