Can';我搞不懂Javascript倒计时

Can';我搞不懂Javascript倒计时,javascript,Javascript,我需要帮助做一个倒计时 用户在文本字段中键入值,单击按钮时计时器启动 计时器文本每秒减少一次,直到达到零 我有第一步的代码,但似乎无法让第二步工作。我知道它需要包含一个setTimeout和循环 以下是迄今为止的代码: HTML- 您需要使用setInterval,而不是setTimeout。将此代码添加到onclick函数: editSec.id = "editSec"; window.cdint = setInterval(function(){ var origVal = par

我需要帮助做一个倒计时

用户在文本字段中键入值,单击按钮时计时器启动

计时器文本每秒减少一次,直到达到零

我有第一步的代码,但似乎无法让第二步工作。我知道它需要包含一个setTimeout和循环

以下是迄今为止的代码:

HTML-


您需要使用setInterval,而不是setTimeout。将此代码添加到onclick函数:

editSec.id = "editSec";
window.cdint = setInterval(function(){ 
    var origVal = parseInt(document.getElementById("editSec").innerHTML);
    document.getElementById("editSec").innerHTML = origVal - 1;
    if(origVal - 1 <= 0){
        window.cdint = clearInterval(window.cdint);
    }
}, 1000);
editSec.id=“editSec”;
window.cdint=setInterval(函数(){
var origVal=parseInt(document.getElementById(“editSec”).innerHTML);
document.getElementById(“editSec”).innerHTML=origVal-1;
如果(初始值-1使用

下面是:

var globalTime = form.value; //receive the timer from the form
var secDiv = document.getElementById("countdown");

function decreaseValue() {
  globalTime = globalTime - 1;

   secDiv.innerHTML = globalTime;
}

startButton.onclick = function (){
  setTimeout(decreasValue(),1000);
};

//clear out the timers once completed

如果需要,可以使用
setTimeout

startButton.onclick = function () {
    var value = parseInt(form.value);
    editSec.innerHTML = value;
    secDiv.appendChild(editSec);
    setTimeout(function () {
        editSec.innerHTML = --value < 10
            ? '<span style="color:red">' + value + '</span>'
            : value;
        if (value) {
            setTimeout(arguments.callee, 1000);
        }
    }, 1000);
};
startButton.onclick=函数(){
var value=parseInt(form.value);
editSec.innerHTML=值;
第二部门附属儿童(编辑部);
setTimeout(函数(){
editSec.innerHTML=--值<10
?“+值+”
:价值;
如果(值){
setTimeout(arguments.callee,1000);
}
}, 1000);
};

如果您愿意,您可以查看我的多重倒计时站点PlentyTime的源代码:我不知道这是否至关重要,但您在调用EditSec后包含了您的.js文件您实际上有两个正文元素,这是无效的!请单击正确答案旁边的复选标记。如果我现在想添加if语句,请o将小于10的数字更改为红色,我将在何处添加它?
var time = 100;
var countdown = function(){
    secdiv.innerHTML = time;
    time--;
}
setInterval(countdown,1000);
var globalTime = form.value; //receive the timer from the form
var secDiv = document.getElementById("countdown");

function decreaseValue() {
  globalTime = globalTime - 1;

   secDiv.innerHTML = globalTime;
}

startButton.onclick = function (){
  setTimeout(decreasValue(),1000);
};

//clear out the timers once completed
startButton.onclick = function () {
    var value = parseInt(form.value);
    editSec.innerHTML = value;
    secDiv.appendChild(editSec);
    setTimeout(function () {
        editSec.innerHTML = --value < 10
            ? '<span style="color:red">' + value + '</span>'
            : value;
        if (value) {
            setTimeout(arguments.callee, 1000);
        }
    }, 1000);
};