The best you can hope for is to have a function run periodically via "setInterval".
I'm not sure what would happen during the infinite loop (as suggested by wilq32), but it would be interesting to test it.
Main Topics
Browse All TopicsWhich commands can be interrupted by event handlers?
There are some javascript commands that can be interupted allowing other javascript procedures to be called (e.g. event handlers).
For example insertBefore can trigger a resize event following the insertBefore command.
I was not aware that a procedure can be interrupted for events and expected the interrupt to be handled after the procedure returned to windows idle.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
txs for all your comments. However some of you are dead wrong. There are some commands that can be interrupted, no matter what the spec says and I have sample code to prove it. In particular insertBefore; and, it can be interrupted following the command to handle a resize event.
I too thought javascript was single threaded so I was surprise also. Nevertheless thank you for your support.
If your issues was not resolved, why did you close the question? And why did you not say something after your initial post?
Awarding a "C" grade is a sure way to rankle us experts. Kind of like biting the hand that feeds you?
For future reference, please list all of the commands that can be interrupted, and show an example illustrating this.
So far there are two commands that will allow a procedure to be interrupted.
document.writeln
and insertBefore.
document.writeln is the easiest to confirm, just create an onunload event for the body and it will fire and code execute after docuement.writeln. After the event is handled the procedure will resume following document.writeln.
I do not requre any more responses to this issue, thanks to all.
@mplungjan,
Thanks! That was what I was referring to in the first paragraph of http:#a25499347 . I may not have worded it well and am glad you posted the details but can't see that it would do what the "insertBefore command" the Asker describes. That was why I, like all of us others it seems, are so curious for some details. We will see what Monday brings. :)
bol
I dont see any problems in code and seeing anything breaking code, but maybe I'm blind. Please correct me if im wrong I will try to follow application flow:
1. First of all we got a frameset caling a function formMain().
2. formMain basically creates a form, then writes to a document then closes it.
3. Closing function calls a onunloadBody function that do this:
sets a whoisfirst global variable to a "onunloadBody" if its not set then alerts it.
Then every new instance of a onclickButton does create form again and closes it, but global variable is already set so no new assignement occures.
I'm just wondering why there is a button still if you close a document there - any explanation for that from experts?
Anyway there is a small problem with context of a whoisfirst variable. I tested this on chrome and got diffrent results so there might be a reference to a window.whoisfirst variable that would be erased every time we creating new form... However when I run this in firefox I did not found out any "code breaking" behaviour using or not a debugger - do I miss something ?
Thank you for your interest.
To the best of my abliity, the previous example works as follows:
1. onunload fires when a form is unloaded
2. main.document.close() does not fire off unload
3. remove 'debugger' if you like, I tested it without it.
formMain builds a form with in the frame named 'main' via commands:
main.document.writeln()
main.document.close()
When the form is displayed there is a button "Unload"
Pressing this button initiates a call to 'onclickButton'
onclickButton which repeats the previous operation and writes over the form with a new one.
Although this is redundant it serves well as the sample code.
Writing over the old form will cause 'onunload' event to fire.
If javascript was not interuptable then after the form is built it will return to onclickButton and
set 'whoisfirst' to 'onclickButton'
whoisfirst is a global and can only be set once (test for set before setting).
However if javascript is interupted and the onunload fires before returning to 'onclickButton' then 'whoisfirst' global would be set to 'onunloadBody' and an alert initiated. And the onclickButton won't be able to set 'whoisfirst'.
The later case is what happens.
A simular condition will happen for insertBefore with the 'onresize' event.
I welcome your feedback or criticism on any 'holes' in my logic.
So first of all can you tell me what browser you test that on?
I test it on firefox 3.5, chrome, IE 7.0 and all of those browsers return diffrent results and does not refeers to language specyfication.
I prepare some more realiable (hopefully) test to run with, because alerts give some strange results (I assume because of some optimalisation method used over firefox).
To answer your question:
"which javascript command can be interupted by event handlers?"
- From language definition - there should be no commands,
- this may vary depends on browser and javascript engine used (basically what is in IE JScript implementation is not a standardized JavaScript that you might ask for - or maybe you dont ask for any standards there ;>)
Business Accounts
Answer for Membership
by: wilq32Posted on 2009-10-02 at 00:33:04ID: 25475759
There is no such a thing like interrupting JavaScript. Basically JavaScript is singlethread - that means that if you want to fire any new function, previous one has to stop. Easiest way to see that is to have a while(1) loop and try to fire a event. Your browse will hang out probably but this is only to visualize. As you noticed insertBefore can trigger some kind of event but this will be fired after whole function that adds this to document, and all actual stack of functions finish..