0

I'm making a little game, and i was making a character death sequence when I ran into this problem. The

eloop(setInterval(e_seq,100)

plays the ending sequence. After that, I want execution to stop for a second before displaying the score and stuff. But the current sleep method i'm using pauses the entire execution, including the loop, while I want the loop to be completed before pausing the game for a second.

The place where sleep is called: (inside the main gameloop)

eloop=setInterval(e_seq,100);
sleep(1000);

The sleep method:

function sleep(msec)
{
var time= new Date().getTime();
while(time+msec>= new Date().getTime())
{}
}

any solutions?

PS: calling sleep at the end of the gameloop (inside an if condition checker) was pausing the execution before the gameloop began for some reason....

2
  • You should read some documentation about setTimeout like this one for example. Commented Mar 31, 2014 at 11:38
  • Check out window.requestAnimationFrame which is an animation loop that's very efficient. It sends a time value with each loop that you can use to get fine-grained control of your death sequence. You can use that time value to calculate an elapsed time which you can use to start-pause-restart your animations. Commented Mar 31, 2014 at 17:03

4 Answers 4

1

I think you probably want something more along the lines of

setTimeout(function () { e_seq(); }, 1000);

This would wait one second and then execute the e_seq() function, which I think is the purpose of your code, although it's open to a little interpretation...

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

Comments

0

Did you try just the setInterval?

setInterval(function(){ ... }, 3000);

Comments

0

i have tried something

var looper;
var looptime = 2000;

var doloop = function(){
    console.log("doing this")   
}

function begin(callthis){
    looper = setInterval(callthis,looptime);    
}

function pause(callthis,sleeptime){
    clearInterval(looper);
    setTimeout(function(){
        looper = setInterval(callthis,looptime);
    },sleeptime)
}

using like:

begin(doloop);

and pause with

pause(doloop,10000);

Comments

0

You need a callback when using "sleep" functionality. The sleep concept does not exist in JavaScript.

You should not use a busy-loop as you do as that will hold off any other processes as well as JavaScript is single threaded (incl. DOM updates). Use a timer instead but as timers are asynchronous you will have to use the mentioned callback.

It's not so complicated -

Modify the sleep method like this:

function sleep(timeout, callback) {
    setTimout(callback, timeout);   // or just call this directly...
}

(as you can see it's a bit excess with the wrapper so I would recommend just calling the setTimeout() directly).

Now you can implement your score screen into a function:

function showScores() {
    ...
}

Then when you want to delay a second before showing the score screen do:

sleep(1000, showScores);

or simply:

setTimeout(showScores, 1000);

Note that the rest of your code will continue after calling this method so make sure all code resides in functions so you can use them as callbacks.

Comments

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.