0

I need to implement a timer in my game. how can i do it? I want to do it using dom and jquery if possible.can someone help?

Edited by techyworld

4
Contributors
6
Replies
8
Views
4 Years
Discussion Span
Last Post by JorgeM
1
window.setInterval(yourfunctionhere, 10000);

function yourfunctionhere() { #do something }
Votes + Comments
spot on
0

Here is a simple JavaScript timer that you can adapt.

<!DOCTYPE html>
<html>
<body>
<div id="myTimer"></p>
<script>
var sec = 5;
var myTimer = document.getElementById('myTimer');
window.onload = countDown;

function countDown() {
        myTimer.innerHTML = sec;        
        if (sec <= 0) {return;}
        sec -= 1;
        window.setTimeout("countDown()", 1000);
}
</script>

</body>
</html>

Edited by JorgeM

0

Hi techyworld,

Hope this helps:

var intervalID;
var gameTimer;
// 1000 mS equivalent to 1 second
var timeInterval = 1000;

gameTimer = function(){
    /** ... do anything here ... **/
    /** set which should be update etc. **/
};

intervalID = setInterval(gameTimer,timeInterval);


/** in case you need to remove the interval function **/
clearInterval(intervalID);

Here's a very simple JS fiddle example too: Online Sample (JSFIDDLE)

Edited by gon1387

0

Here is an updated version of what I previously posted. This example can be adapted in a web site that shows a count down timer prior to letting you proceed, submit, download, etc...

http://jsfiddle.net/u9zCa/

In the example, the button is disabled until the counter reaches 00 seconds.

sample4

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.