Javascript 在var使用setinterval达到某个值后做些什么?
我有一个引导加载条,每250ms更新一次setinterval,所以看起来它正在进步。我试图在加载条达到100%时显示警报和重定向Javascript 在var使用setinterval达到某个值后做些什么?,javascript,twitter-bootstrap,timer,setinterval,Javascript,Twitter Bootstrap,Timer,Setinterval,我有一个引导加载条,每250ms更新一次setinterval,所以看起来它正在进步。我试图在加载条达到100%时显示警报和重定向 <div class="progress" style="border-radius:0px;"> <div class="progress-bar progress-bar-success progress-bar-striped active" style="border-radius:0px;" id="loadi
<div class="progress" style="border-radius:0px;">
<div class="progress-bar progress-bar-success progress-bar-striped active"
style="border-radius:0px;" id="loading" role="progressbar" aria-valuenow="75"
aria-valuemin="0" aria-valuemax="100" style="width: 0%">
</div>
</div>
您需要一个if语句来检查条是否已满,如果已满,请清除间隔,显示警报,最后执行重定向 有用功能:
clearInterval
停止计时器,添加if
语句以检查%是否已达到100,并添加location.href
以重定向URL
var percentl = 10;
var timer = setInterval(function () {
percentl = percentl + 2;
document.getElementById("loading").style.width = percentl + "%";
if(percentl > 99){
clearInterval(timer);
// do your stuff
location.href = "redirectToThisURL";
}
}, 250);
var percentl = 10;
var timer = setInterval(function () {
percentl = percentl + 2;
document.getElementById("loading").style.width = percentl + "%";
if(percentl > 99){
clearInterval(timer);
// do your stuff
location.href = "redirectToThisURL";
}
}, 250);