I’m working on a strictly javascript based game. What I want is for certain bits of information be printed out to the screen in regular intervals. Easy enough? Yes… but the problem is that I only want to delay how some text is printed out to the screen (adding text to the innherHTML of a div).
I’m aware of setInterval and setTimeout methods, but the problem with those is that it only delays how long it takes for a certain function to be called while the rest of the script continues to run. What I want is for everything to pause, to not allow javascript to advance at all until a certain duration has passed.
I can think of a workaround–instead of writing straight to the innerHTML property, write to a queue of strings, then use a setInterval method to display a line of text if there is anything waiting in the queue. Not hard to do, but I’m wondering if Javascript has any kind of pause-all command that will do the same thing. For now I’m avoiding jQuery.
Thanks!
Nope, there is no such thing. Javascript is asynchronous and you can’t stop other “threads”. What you can do is to have some global variable or main program class method that stops the game processes.
Simple pauseable game class: