1

I've got a javascript loop that triggers every time the page receives an update via websocket. This loop persists usually even after the next websocket update is received. The result is multiple loops begin running and page performance degrades rapidly. Each loop is critical though and only becomes obsolete until the next update.

Is there a way to terminate an active loop from another function or am I plainly just thinking about this the wrong way?

1
  • I think you're thinking about it the wrong way. Unless there are "web workers" involved, your browser JavaScript environment is strictly single-threaded. It's therefore not possible for a single page to have multiple event loops running concurrently. Commented Dec 1, 2011 at 23:55

2 Answers 2

1

You have a conditional in your loop that looks for a global flag, and when the update is received set the flag to False.

Sign up to request clarification or add additional context in comments.

7 Comments

How is any other JavaScript code going to set a flag while the loop is running in a single-threaded environment (like a browser)?
@Pointy Think about it. If a browser was a single-threaded environment per se, you could not access the main menu while a document was being loaded. You could not read the document while stylesheets are processed. A browser is certainly not single-threaded in any meaning of the word. Client-side script execution usually is. So you can have one script engine thread to influence another script engine thread if they share the same runtime environment, as in this case. That thread could be an event thread or created with window.setTimeout(…).
@PointedEars the point is that JavaScript in a browser is single-threaded (per-window or per-tab). Outside of web workers, things are not multi-threaded in a browser, and that includes web socket interaction (as far as I know).
@Pointy First, be sure what you call "JavaScript". Also, it is not specified that ECMAScript implementations (in browsers) are always single-threaded; we only have observation in browsers to make an educated guess here. From what I have found follows an event listener model. Such models are part of host objects. It is possible and makes sense for a server implementation that for each listener call a new script engine thread in the same runtime environment is created.
@PointedEars I realize that there are different implementations of JavaScript, but I give you a 100% ironclad guarantee that, setting aside the new "web worker" feature, all browsers run JavaScript single-threaded, and they always have. This question is almost certainly about JavaScript in a browser ("... every time the page receives an update ...").
|
0

Usually you would set a flag/condition outside of the loop that get's checked on every iteration.

So..

while(true){
    // Do stuff
    if(condition == false){
       break;
    }
}

In your case I think what you could have is a queue of updates. So your loop would check the queue - see that it has four updates since it last did it's thing - then it does whatever it does. New websocket updates come in - loop finishes what it was doing - comes back and checks the queue again.

5 Comments

Exactly what I needed, many thanks for the help. I'll accept it when SO allows me :)
Web sockets don't give you multi-threading, do they? How would any code set a flag while this event loop thing is running?
The way I was able to handle this was I set a global variable that held a timestamp related to the most recent update. Then the "flag" checked to see if that loop matched the global timestamp.
@Pointy I'll have to double-check multi-threading, but simply I was triggering multiple javascript functions that were crashing the page.
Surely that would be: while (condition) {...} where condition is available to an outer scope and can be modified independently. It might need to be an expression since simply toggling a boolean might not be enough, e.g. give each instance of the update function a unique id and when starting a new one, change the current id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.