1

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)

Jacksonkr
  • 31,583
  • 39
  • 180
  • 284
  • 1
    Could you give us an idea of the nature of the data you are looping, or what happens in each loop iteration? Are you modifying DOM, parsing strings... whatchya doin'? A code sample would be just berries. – Chris Baker Feb 23 '11 at 18:19
  • 1
    @Jackson we'll need more information than that..... are you asking to kill the IE 7/8 prompt or to help with loop performance? – DefyGravity Feb 23 '11 at 18:19
  • 3
    If you had included the code with your question you'd probably have an answer by now :-) – Pointy Feb 23 '11 at 18:20
  • @Jackson Apart from possibly moving some property accesses into local variables, without any idea of what the loop contains the question is of the form "how long is a piece of string?" – jmbucknall Feb 23 '11 at 18:21
  • 1
    With this much info all I can think of to fix your problem is `` – the JinX Feb 23 '11 at 18:23
  • I made an edit which adds code. This code works correctly. IE just whines because the code can take up to 6 seconds to run (only in IE7/8, others take about .002 - 2 seconds). I merely wish to somehow suppress the error message in IE as it's the only one with the issue. – Jacksonkr Feb 23 '11 at 18:28
  • hah, @JinX +1 for comic relief. – Jacksonkr Feb 23 '11 at 18:36

2 Answers2

2

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.

rsp
  • 107,747
  • 29
  • 201
  • 177
  • Good call. I might try this (or something like it). I was originally wondering if I should go for a recursive function. either way, Thanks for the suggestion! – Jacksonkr Feb 23 '11 at 18:33
0

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).

Thomas Sampson
  • 417
  • 4
  • 13
  • Not IE 8 or below, you'll need to wait for IE9 . . – the JinX Feb 23 '11 at 18:24
  • Pretty cool (didn't know about these), but unfortunately I need a solution which is graceful when applied to IE7/8 as @JinX mentioned. However, I'm going to keep this solution in mind for future project. Thanks. – Jacksonkr Feb 23 '11 at 18:36
  • No problem, I have only ever used them on a recent project where the client required only compatibility with Google Chrome. – Thomas Sampson Feb 23 '11 at 18:44