2016-01-14 37 views
0

这里是链接到我的代码http://codepen.io/meow414/pen/adLoEY请看看。sec = 0时sec变量不显示0,它在sec == 0但在递减后不显示0,如何显示它。为什么零在输出递减后没有显示?它是超时程序

var min = 0; 
var sec = 3; 
var over; 

function xyz() { 
    document.getElementById("abc").innerHTML = min + ":" + sec; 

    sec--; //it is getting decremeted upto 0,that's why below while loop is running bu in output zero is not shown for ex 3 after decrementing will show like 3 then 2 then 1 then it will chnage to 9. 

    while(sec == 0) { 
     sec = 9; 
    } 
} 

$("#start").click(function() { 
    over=setInterval(xyz, 1000); 
}); 

$("#pause").click(function() { 
    clearInterval(over); 
}); 

$("#reset").click(function() { 
    min = 0; 
    sec = 5; 
    clearInterval(over); 
    document.getElementById("abc").innerHTML = min + ":" + sec; 
}); 
+0

请重新表述您的问题 - 目前还不清楚发生了什么。此外,改变你的标题,所以它不那么长(你不需要在标题中发布整个问题) – AMACB

+0

对不起,我匆忙让我改变, –

回答

2

你的函数使用while作为条件(而不是if)。

此外,当您将sec从1更改为0时,您立即将该值设置为9。

尝试

function xyz() { 
    // Show sec, even if it's 0 
    document.getElementById("abc").innerHTML = min + ":" + sec; 
    // Then if(sec===0) set sec to 9 (the HTML will still show 0) 
    if (sec === 0) { 
    sec = 9; 
    } 
    // Otherwise, decrement sec 
    else { 
    sec--; 
    } 
} 

CodePen:http://codepen.io/anon/pen/rxGBbd

+0

我不能放在那里,我用很多if在原始程序中的其他条件,他们都需要感谢sec。 –

相关问题