2016-01-14 83 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。 –

相關問題