如何启动setTimeout() [英] How to start setTimeout()

查看:107
本文介绍了如何启动setTimeout()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立javascript计时器。当我第一次启动时它工作正常但是当我停止它时我无法启动它第二次,请告诉我在我的代码中做了什么错误。

java脚本代码:

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代码

消耗时间

< input name = dispvalue =00:00:00readonly =readonlyid =dispclass =time_texttype =text

style =border-style:none/> ;



< input id =Button1type =buttonvalue =start/>

< input id =Button2type =buttonvalue =stop/>

< input id =Button3type =buttonvalue =reset/>

< input id =Button4type =buttonvalue =get/>



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" />

推荐答案

首先,代码仅适用于FF和从我的测试IE浏览器。



为了让计时器工作最初添加在HTML中

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" />



但这不是问题,问题在于函数名称cd和使用的相同变量cd用于引用setTimeout。

更改函数或变量名称:


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();
 }



还有为什么使用cookie而没有直接从disp输入字段获取时间值的任何特定原因?


Also any particutlar reason why cookies are being used and not getting the time value directly from the "disp" input field?


这篇关于如何启动setTimeout()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆