Jump to content

timer for game


kadi
 Share

Recommended Posts

i'm using the code below but its going below 0 when it is in use or running in background. Everything else is perfect. i am testing on an ios mobile device and when i minimise and then resume, the timer is still running in background. when it is not in use or minimised, the timer needs to stop and when resumed it needs to start again. most probably like a game timer should stop at 0.Hope someone can help me on this

<span id="count" ></span><script type="text/javascript">/*<![CDATA[*/var zxcMatch={ Reset:function(id){  var o=this[id],z0=0;  if (o){   clearTimeout(o.to);   o.time[1]=null;   for (;z0<o.imgs.length;z0++){    o.imgs[z0].style.visibility='visible';   }   o.cnt=z0/2;   o.lst=null;  } }, init:function(o){  var id=o.ParentID,imgs=document.getElementById(id).getElementsByTagName('IMG'),z0=0;  o.imgs=imgs;  for (;z0<imgs.length;z0++){   this.addevt(imgs[z0],'mouseup','match',o,imgs[z0]);  }  o.time=[typeof(o.Timer)=='function'?o.Timer:null];  o.cnt=z0/2;  this[id]=o; }, match:function(o,img){  if (o.time[0]&&!o.time[1]){   o.time[1]=new Date();   o.to=setInterval(function(){ o.time[0](o,Math.floor((new Date()-o.time[1])/1000)); },1000);  }  if (!o.lst){   o.lst=img;  }  else {   if (o.lst.className==img.className&&o.lst!=img){    img.style.visibility=o.lst.style.visibility='hidden';    o.cnt--;    if (o.cnt==0){     clearTimeout(o.to);     o.time[1]=null;     o.Complete();    }   }   else {    alert('try again');   }   o.lst=null;  } }, addevt:function(o,t,f,p,p1){  var oop=this;  o.addEventListener?o.addEventListener(t,function(e){ return oop[f](p,p1);},false).attachEvent?o.attachEvent('on'+t,function(e){ return oop[f](p,p1); }):null; }}zxcMatch.init({ ParentID:'match-holder', Timer:function(o,sec){  document.getElementById('count').innerHTML=30-sec;  if (sec>29){    alert('Time Out');    zxcMatch.Reset('match-holder');  } }, Complete:function(){//  window.top.location='http://www.vicsjavascripts.org.uk/'; }});/*]]>*/</script>
Link to comment
Share on other sites

It looks like you're already testing for a time out, you can clear the timeout interval at that point to stop the clock. You might be able to use window.onblur to detect when the browser gets minimized, it should also fire if the browser just loses focus.

 

https://developer.mozilla.org/en-US/docs/Web/API/window.onblur

Link to comment
Share on other sites

If I were you I would write a simple test-case to determine what happens on minimize and resume, and what events are available for that.

 

so can u please do a sample test case code. i'm testing on ios device. i'm using phonegap ...

Edited by kadi
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...