簡體   English   中英

使用setinterval在var達到某個值后執行某些操作?

[英]Do something after var reaches certain value with setinterval?

我有一個bootstrap加載欄,每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="loading" role="progressbar" aria-valuenow="75" 
         aria-valuemin="0" aria-valuemax="100" style="width: 0%">
   </div>
</div>

我已經嘗試在函數結束之前添加window.location但它不起作用。 我怎樣才能做到這一點? 也許用if語句?

        var percentl = 10;
        setInterval(function () {
            percentl = percentl + 2;
            document.getElementById("loading").style.width = percentl + "%";
        }, 250);

您需要一個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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM