1

I have a script that I need to bump out of the javascript execution queue. I have found that I can do this with a couple methods.

alert();//of course we can't use this one.

setTimeout(function(){
    someScript();//works, but are there vulnerabilites?
}, 1);

What other methods are there and what is the correct way to bump out of the javascript execution queue?

If setTimeout is the best option, what are the vulnerabilities of using setTimeout? Are there possible future problems, possibility that the code in the timeout won't get called, speed issues, etc.

Mechlar
  • 4,946
  • 12
  • 58
  • 83
  • not sure what you mean by bump out. Also, in your comment where you say 'using jQuery here', that's not jQuery. That's just regular JavaScript, no jquery needed. – Matt Dec 02 '10 at 21:38
  • when you say "vulnerabilities" are you asking for things to watch out for? (variable scope, context, etc) – zzzzBov Dec 02 '10 at 21:40
  • @Matt, The browser queues javascript calls. By bump out I mean, get out of that queue, and requeue. The comment is for what's in the function I call. Forgot to remove it for this example. – Mechlar Dec 02 '10 at 21:42
  • @zzzzBov, by vulnerabilites I mean possible future problems, possibility that the code in the timeout won't get called, speed issues, etc. – Mechlar Dec 02 '10 at 21:44
  • 1
    It's technically possible that the timeout wont get called if the browser is closed before the timeout executes, or if the timer is canceled between the setTimeout call and execution. As far as I know setTimeout works well. – zzzzBov Dec 02 '10 at 21:49
  • http://ejohn.org/blog/how-javascript-timers-work/ – Alfred Jul 15 '12 at 12:49

3 Answers3

3

setTimeout is the typical way of interrupting the execution flow. alert() is not the same thing, it is synchronous - it will stop your code until OK is pressed, and resume where it left off. When you use setTimeout, that frees up the thread to go execute another section of code.

The only vulnerabilities are not knowing what you are doing. Coding async is a little trickier than coding sync. And you have to watch your context, because using "this" won't work:

object = {
    firstMethod: function(){
        setTimeout(function(){
            this.secondMethod(); // won't work!
        }, 30);
    },
    secondMethod: function(){}
}
mwilcox
  • 4,065
  • 23
  • 20
  • 1
    Your answer could be interpreted that there's no way to execute 'secondMethod' from the timeout function context. I recommend to update to reflect that it's still possible. – alex Dec 06 '10 at 23:48
1

Basically you are talking about executing out of current stack, in other words asynchronously. setTimeout() is the way to go in this case and the code after setTimeout() will be executed before the callback function.

1ms in setTimeout() never works, however in terms of actually being 1ms. The fastest you can usually get is around 10ms in my experience.

alex
  • 2,036
  • 2
  • 16
  • 19
1

setTimeout is the way to do it. Just note that the second parameter is timeout in miliseconds not seconds

code_burgar
  • 12,025
  • 4
  • 35
  • 53