javascript - Using requestAnimationFrame as a Timer -


this multi-player "guess word" turn-based game working on.

i have page supposed count down given number. if word-guess submitted before time limit, signalr hub method called. if 0 reached, different signalr hub method called. in both cases, hub method sends message out relevant clients (in group).

someone recommended, instead of js timer, can use requestanimationframe updates every 17 milliseconds.

so here requestanimationframe code have in play.aspx file...

function turntimeron() { //called server code startup script        var timerstep = '<%=application(session("gameid") & "_timelimit")%>';       timerstep = number(timerstep);        var startt = date.now();       var timedifference = 0;       var countdown = document.getelementbyid('secondsleft');       requestanimationframe(function anim() {                requestanimationframe(anim);                startt = startt || date.now();                countdown.innerhtml = timerstep - ((date.now() - startt) / 1000 | 0);                timedifference = (date.now() - startt) / 1000 | 0;                 if (timedifference >= timerstep) {                        startt = date.now();                       timedifference = 0;                        countdown.innerhtml = "";                       tomatohubproxy.invoke('nextplayer', '<%=session("username")%>', '<%=session("gameid")%>');                  }             });       } 

the return message signalr hub causes execution of turntimeron() again, , meant start countdown again current player.

the count down seems work has bug reaches low number 3 or 4 , either resets count down or invokes hub method (it's hard tell happening). supposed invoke hub method when count down reaches 0 (so game can move next player)

i should mention play.aspx page never re-loaded. updates happen automatically (thanks signalr)

please help.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -