I need to create a javascript countdown timer, I've been trying to do it using setInterval method but when I pass the seconds as a parameter it keeps looping so that it only goes down by 1 second then stops. Here is my code:
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
Thanks!
just do:
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);
Demo:: jsFiddle
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
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.