我有一个倒数计时器,从10倒数到0。我想在倒数计时器到期后设置新的标头...
var timeleft = 10;
var downloadTimer = setInterval(function(){
document.getElementById("countdown").innerHTML = timeleft;
timeleft -= 1;
if(timeleft <= -2){
clearInterval(downloadTimer);
document.getElementById("countdown").innerHTML = "0"
}
}, 1000);
任何人都知道实现这一目标的最简单方法吗?
答案 0 :(得分:0)
如果您要向页面添加新元素(例如<h1>
),请尝试以下操作:
var timeleft = 10;
var downloadTimer = setInterval(function() {
document.getElementById("countdown").innerHTML = timeleft;
if (timeleft == 0) {
clearInterval(downloadTimer);
document.getElementById("countdown").innerHTML = "0";
// create new h1 element
var h1 = document.createElement('h1');
h1.innerHTML = "Your header text";
// and here append it to the page before the timer
var timer = document.getElementById("countdown");
var parent = timer.parentNode;
parent.insertBefore(h1, timer);
}
timeleft -= 1;
}, 1000);
或者如果您已经有一个标头,并且只想为其设置新文本,那么在“ // create new h1 element”点,只需按id获取现有的并将其设置innerHTML即可:
document.getElementById("header").innerHTML = "Your new header text";
希望它能帮助:)
答案 1 :(得分:0)
var timeleft = 10;
var downloadTimer = setInterval(
function(){
document.getElementById("countdown").innerHTML = timeleft;
if(timeleft <= 0){
clearInterval(downloadTimer);
document.getElementById("countdown").innerHTML = "0";
// jump_to_your_next_function() or
clearInterval(downloadTimer);
}
timeleft -= 1;
}, 1000
);