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?

- 72,912
- 30
- 168
- 299

- 431
- 5
- 17
-
1Please provide us some code so that we may be able to provide better answers. – Aadit M Shah Oct 19 '14 at 03:02
-
You may also want to see web workers, if you are just make calculations. http://www.html5rocks.com/en/tutorials/workers/basics/ – GramThanos Oct 19 '14 at 03:03
-
3I think [function*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*) is what you want. For es5 compatible function, you can mock `yield` by switch case. – Herrington Darkholme Oct 19 '14 at 03: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. – Suo6613 Oct 19 '14 at 03: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 – Paul Oct 24 '14 at 03:53
2 Answers
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.

- 11,901
- 2
- 38
- 67
-
3`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) – tckmn Oct 19 '14 at 03:02
-
2if any part takes more than 100ms to be executed, the global function would break. – rnrneverdies Oct 19 '14 at 03:03
-
-
@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."](http://blog.carbonfive.com/2013/10/27/the-javascript-event-loop-explained/) – Fengyang Wang Oct 19 '14 at 13:59
-
@FengyangWang ...oh, nice! tanks for teaching! i still thinking in c++.. apologize. – rnrneverdies Oct 19 '14 at 14:11
-
This code releases the asynchronous programming genie upon the unwary. While this code might seem to answer the OP's question, I think this code might not play nice with other code that assumes both parts of the function have executed when the function returns. – Paul Oct 24 '14 at 03:27
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.

- 868,454
- 176
- 1,908
- 1,964
-
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. – Aadit M Shah Oct 19 '14 at 02:41
-
The OP used the setinterval tag. That could perhaps be corrected. – Fengyang Wang Oct 19 '14 at 02:45
-
@Aadit M Shah - Well if you read it again, you will see that he is confusing `setTimeout` and `setInterval`. – GramThanos Oct 19 '14 at 02:54
-
@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. – Aadit M Shah Oct 19 '14 at 02:57
-
-
Thank you very much. I think, yes I am confused with these things and would be grateful if you guys can help me. Actually, I am using PhantomJS to crawl a website. I have bunch of functions. I want to stop all my functions right after triggering each request and resume my function (right after the line on which I had stopped my functions) after receiving all the responses. Therefore, I don't know which function will trigger the next request. I don't want my functions to be executed every 100 milliseconds. I mean, I don't want to have any delay here. – Suo6613 Oct 19 '14 at 03:17
-
But, using your definition it seems that I am using delay here. I have too many lines of code and I don't know which part can help me to explain my question better. Is my question clear or you need me to explain it in different way? – Suo6613 Oct 19 '14 at 03:18
-
To clarify, I have a main function. let's say 'myFunction' and I call my other functions inside 'myFunction'. Then, I can pause 'myFunction' using 'setTimeout' but I want to resume it exactly from the function that caused it to be stopped! – Suo6613 Oct 19 '14 at 03:21
-
@Suo6613: You can't "pause" a function. You should use promises to wait for responses to arrive once you need them. – SLaks Oct 19 '14 at 03:26