Hi i need a simple timer for seconds which keep increment.
0 1 2 3 ....30
Can anyone help?

Recommended Answers

Not Tested:

var timer = {

    // ID of div to display the time
    divId: "myDivId",

    // Used to store the div object
    div: undefined,

    // Used to store the interval object
    interval: undefined,

    // Used to store the current time
    currentTime: 0,

    // Initialize timer
    init : function() …
Jump to Post

All 2 Replies

Not Tested:

var timer = {

    // ID of div to display the time
    divId: "myDivId",

    // Used to store the div object
    div: undefined,

    // Used to store the interval object
    interval: undefined,

    // Used to store the current time
    currentTime: 0,

    // Initialize timer
    init : function() {

        timer.div = document.getElementById(timer.divId);

        timer.interval = setInterval(function() {

            timer.currentTime = currentTime + 1;

            timer.updateTimeDisplay();

        }, 1000); // 1000 milliseconds = 1 second
    },

    updateTimeDisplay: function() {
        timer.div.innerHTML = timer.currentTime + " seconds";
    },

    stop: function() {
        clearInterval(timer.interval);
    }

};

// Initialize timer
timer.init();


// Stop timer after 100000 milliseconds
setTimeout(function() {

    timer.stop();

}, 100000);
Be a part of the DaniWeb community

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