问题
I have a loop execution that needs to run in all browsers.
in chrome/ff etc the execution runs fast and fine. in IE it's slow and end's up dispatching a prompt saying a script is running slow (no good).
Any ideas on how to get around something like this? I mostly just need to get rid of the IE prompt for 7/8
** edit **
Here's code:
if(this.handicap()) {
while(this.hasGraphChanged()) {
this.gravity(this.separate());
}
}
This is a VERY large project, so instead of listing all the code, I'll go for a quick explanation.
this.handicap: returns true if the browser if IE7/8 this.hasChanged: returns true/false depending if there is a change AFTER a draw update this.gravity: processes drawing algorithm based on p1(array)
回答1:
You can use some asynchronous iteration technique instead of loops. Watch Asynchronous Iteration Patterns by Pedro Teixeira for a nice introduction. It uses Node.js but you can use the same patterns in the browser.
回答2:
What exactly does the loop do? Is the number of iterations deterministic? If this is something which is causing the browser to hang you may want to consider javascript worker-threads https://developer.mozilla.org/En/Using_web_workers (although I'm not sure which browsers currently support this feature).
来源:https://stackoverflow.com/questions/5095161/loop-is-too-slow-for-ie7-8