Click here to Skip to main content
14,669,305 members
Rate this:
Please Sign up or sign in to vote.
See more:
I wants to buils javascript timer. when I start it first time it works fine but when i stops it I can't start it second time, please tell me what mistake I done in my code.
java script code:
var hrs
var mins
var secs;

var date = new Date();
function createCookie(name, value) {
    var expires;
    var days = 1;
    if (days) {
        //var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    }
    else {
        expires = "";
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) {
                c_end = document.cookie.length;
            }
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

function cd() {
    //var strTime = document.Form1.disp.value;
    var strTime = getCookie("zz");
    tme = strTime.split(":");
    hrs = 0 * h(tme[0]);
    mins = 0 * m(":" + tme[1]); // change minutes here
    secs = 0 + s(":" + tme[2]); // change seconds here (always add an additional second to your total)
    redo();
}

function h(obj) {
    for (var i = 0; i < obj.length; i++) {
        if (obj.substring(i, i + 1) == ":")
            break;
    }
    return (obj.substring(0, i));
}

function m(obj) {
    for (var i = 0; i < obj.length; i++) {
        if (obj.substring(i, i + 1) == ":")
            break;
    }
    return (obj.substring(i + 1, obj.length));
}

function s(obj) {
    for (var i = 0; i < obj.length; i++) {
        if (obj.substring(i, i + 1) == ":")
            break;
    }
    return (obj.substring(i + 1, obj.length));
}

function dis(hrs, mins, secs) {
    var disp;

    if (hrs <= 9) {
        disp = " 0";
    } else {
        disp = " ";
    }
    disp += hrs + ":";

    if (mins <= 9) {
        disp += "0";
    } else {
        disp += "";
    }
    disp += mins + ":";

    if (secs <= 9) {
        disp += "0" + secs;
    } else {
        disp += secs;
    }
    return (disp);
}

var tm;
function redo() {
    secs++;
    if (secs == 60) {
        secs = 00;
        mins++;
    }
    if (mins == 60) {
        mins = 00;
        secs = 00;
        hrs++;
    }


    document.Form1.disp.value = dis(hrs, mins, secs); // setup additional displays here.
    createCookie("zz", dis(hrs, mins, secs));
    cd = setTimeout("redo()", 1000);

}
function startTime() {
    cd();

}
function stopTime() {
    clearTimeout(cd);
}
function resetTime() {
    createCookie("zz", "00:00:00");
}
function getTime() {
    alert(getCookie("zz"));
}

function init() {
    document.Form1.disp.value = getCookie("zz");
}
window.onload = init;


html code
Time Consumed
<input name="disp" value=" 00:00:00" readonly="readonly" id="disp" class="time_text" type="text"
style="border-style:none"/>

<input id="Button1" type="button" value="start" />
<input id="Button2" type="button" value="stop" />
<input id="Button3" type="button" value="reset" />
<input id="Button4" type="button" value="get" />
Posted
Updated 4-Nov-14 0:50am
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Firstly the code only works in FF and IE from my testing.

To get the timer to work initially added in html
<input id="Button1" type="button" onclick="javascript:startTime();" value="start" />
<input id="Button2" type="button" onclick="javascript:stopTime();" value="stop" />
<input id="Button3" type="button" onclick="javascript:resetTime();" value="reset" />
<input id="Button4" type="button" onclick="javascript:getTime();" value="get" />

But that is not the problem, the issue is with the function name cd and the same variable cd being used for the referencing of the setTimeout.
Either change the function or variable name:
//function cd()
function cdFunc() {
    //var strTime = document.Form1.disp.value;
    var strTime = getCookie("zz");
    tme = strTime.split(":");
    hrs = 0 * h(tme[0]);
    mins = 0 * m(":" + tme[1]); // change minutes here
    secs = 0 + s(":" + tme[2]); // change seconds here (always add an additional second to your total)
    redo();
}
function startTime() {
    //cd();
    cdFunc();
 }

Also any particutlar reason why cookies are being used and not getting the time value directly from the "disp" input field?
   
Comments
Sid_Joshi 5-Nov-14 0:44am
   
It Works..
Thank you for your solution...
jaket-cp 5-Nov-14 4:09am
   
glad to help, but you do need to work on the code to get it to work in Chrome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100