我需要创建一个javascript倒计时器,我一直在尝试使用setInterval方法,但是当我将秒作为参数传递时,它会保持循环,这样它只会下降1秒然后停止。这是我的代码:
JavaScript:
var second = 120;
document.getElementById('timer').innerHTML = second;
function setTimer(second)
{
second--;
document.getElementById('timer').innerHTML = second;
return second;
}
second = window.setInterval(setTimer, 1000, second); //Timer for user display
谢谢!
答案 0 :(得分:2)
只是这样做:
function setTimer(num) {
//start the interval
var counter = setInterval(function () {
document.getElementById('timer').innerHTML = num; //write to div
num-- || clearInterval(counter); //clear (stop) if its 0
}, 1000);
}
setTimer(120);
演示:: jsFiddle
答案 1 :(得分:0)
var second = 120;
var timeOutFunction;
document.getElementById('timer').innerHTML = second;
function setTimer()
{
second--;
document.getElementById('timer').innerHTML = second;
if(second == 0)
clearInterval(timeOutFunction);
}
timeOutFunction = setInterval(setTimer, 1000); //Timer for user display
答案 2 :(得分:0)
var array1 = ['one', 'two', 'three', 'four', 'five'];
var array2 = ['two', 'four'];
for (var i = array2.length -1; i >= 0; i--)
array1.splice(array2[i],1);
console.log(array1);