Jump to content

wait timer function not working


Rocco
 Share

Recommended Posts

i tried it this way, but it's not working:

time_wait: function(msec)   {               var time_finish = this.time.now + msec;        while(this.time.now < time_finish) { }              return true;    }

Seems that game.time.now isn't updating.

Any suggestions on how i get it to work?

 

Link to comment
Share on other sites

a while loop wait is very bad solution it'll block your code and load CPU at 100%

This is how I do it

 

function wait(condition, callback, maxwait, tick) {	if (typeof condition != 'function' || typeof callback != 'function')		return false;	var _tick = tick || 100;	var startDate = new Date().getTime();	var itv = setInterval(function () {		if (condition()) {			clearInterval(itv);			callback();		}		if (maxwait) {			var date = new Date().getTime();			if (date - startDate >= maxwait)				clearInterval(itv);		}	}, _tick);	return true;}

Condition is a function indicating when the wait condition is met

Callback is a function to execute when "condition" is met
maxwait is the max time after witch you stop waiting

tick is the interval check in milliseconds (if not set = 100)


usage
 

var count = 0;... //some code ...wait(    function() { return count = 100} ,   // the stop condition is when count reatch 100    function() {                        //this is my callbaclk        do_something();     },     30000,    // abandon if count is not equal to 100 after 30 seconds     30     //check every 30ms);

the above code will wait condition and execute code, and is non blocking

Link to comment
Share on other sites

Can't you just use the Timer class then? Also to the OP, a non blocking way is the way to go but it raises other issues for you I'm sure because now you have to handle the state things are in so that code doesn't continue along it's normal execution until your wait is over. You have to restructure your code for non blocking (unless coroutines are supported in Javascript which I'm not sure. I come from Lua which they are.).

 

If you are thinking of coding like:

MoveHere();Wait(5000);SayThis();Wait(2000);etc

Then you have to code this differently.

Link to comment
Share on other sites

sorry if i have annoyed you george.

it may seem as the same question but it isn't in my opinion.

my wrong approach is the same in both ways, but what i want to achieve is different.

 

In this case i want a working Wait function, with isn't working with my approach,

but i'm courios and still thankful, if someone could help me with a working approach.

So thank you Ezelia for you example.

Its very interesting as non-blocking function and for sure useful for me and others but i was looking for a way rpiller was mentoned right now.

Link to comment
Share on other sites

oh sorry rocco, my fault. I was so perplex to see the while loop again.

I always and only use the phaser method to create a timeout. This ensures that it's paused when the game is paused (when unfocussed)

 

game.time.events.add(500, function(){ console.log('timeout complete') } );

This is an integrated solution of ezelias wait solution. I hope that helps. And after successful using the onComplete signal of a tween you should be comfort with a callback situation!

 

Regards George

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...