timer - Stop time automatically in Javascript using clearTimeout() -
i creating pong game using webgl, trying when ball touches wall want stopwatch stop automatically. here code of timer:
var h4 = document.getelementsbytagname('h4')[0], seconds = 0, minutes = 0, hours = 0, t; function add() { seconds++; if (seconds >= 60) { seconds = 0; minutes++; if (minutes >= 60) { minutes = 0; hours++; } } h4.textcontent = (hours ? (hours > 9 ? hours : "0" + hours) : "00") + ":" + (minutes ? (minutes > 9 ? minutes : "0" + minutes) : "00") + ":" + (seconds > 9 ? seconds : "0" + seconds); } function timer() { t = settimeout(add, 1000); }
and here code when want stop timer, can stop ball how stop timer?
if (ball.position.x <= -fieldwidth/2 && ball.position.x >= fieldwidth/2){ ballspeed = 0; ... }
just call functioncleartimeout(myvar);
, myvar
in sample t
on w3schools.com have example http://www.w3schools.com/jsref/met_win_cleartimeout.asp
Comments
Post a Comment