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?

Recommended Answers

All 6 Replies

window.setInterval(yourfunctionhere, 10000);

function yourfunctionhere() { #do something }
commented: spot on +5

did it help?

not really :(

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>

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)

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.