Forum Moderators: phranque
I once spent a couple weeks researching how to multithread javascript functions in IE for all the reasons above, and the closest I could come up with I wanted to share with you: setTimeout()
Basically, you want to separate the process from the rest of the flow of the script (where appropriate), for instance to update the screen before popping an alert with results, and this is the way to do it:
dothis(); // 1 normal processes...
dothat(); // 2 normal processes...
setContent(); // 3 post some stuff to the screen..
doBigProcess(); // 4 lengthy process...
In the above scenario, 1,2,3, and 4 need to complete execution before 3 visually takes effect. What we would prefer, is the 1, 2, and 3 happen, and Then 4 happens.
Split 4 out:
dothis(); // 1 normal processes...
dothat(); // 2 normal processes...
setContent(); // 3 post some stuff to the screen..
setTimeout("doBigProcess();", 0); // 4 lengthy process...
The parameters to setTimeout(str, int) are the string to interpret and execute and the delay before which it will wait to take effect (in milliseconds). I use 0 so I don't waste any time, but I do take advantage of splitting off the process.
cheers,
bronius
Not sure i understand this, though:
>> In the above scenario, 1,2,3, and 4 need to complete execution before 3 visually takes effect
Anyway, you can make the progressbar, or a count in window.status by eg. splitting the processes up in tiny bits separated by a settimeout-loop (settimeout calling itself). You have to include a counter though, in order to break the loop when finished, but then you can write 10%, 20% etc to the status line while the code executes...Hope this is understandable, else i'll post some pseudocode ;)
/claus
<edit>
i'll rephrase that, didn't find it very clear myself:
- hope this made more sense. SetTimeout does not call itself, it just get called a couple of times, introducing some buffer time, enough to write to window.status.
</edit>
[edited by: claus at 10:15 pm (utc) on July 21, 2003]
When the hidden iframe is done processing, the onload event of the body tag is called in the iframe page. In the onload event, call window.parent.finish() - which is essentially a "callback" to the parent page.
If you are using IE only, you can do a lot of stuff with innerhtml to reload stuff into divs on the parent page. Always wows the client ;)
var pframe = document.createElement('<iframe id="processFrame" style="display:none">');
pframe.document.write("somecode");
which would allow you to conveniently create a jscript snippet or include to add this process-split funcitonality.