Jquery延遲函數


1.setInterval(function(){},[time]) 

示例:這是一個模擬進度條的代碼,結束后轉向另一頁面。

<script type="text/javascript">
    $(function () {
        var i = 0;
        var t = 0;
        setInterval(function () {
            if (i < 100) {
                i = i + 5;
                t++;
                $("#linepercent").css("height", i + "%");
                $("#linetime").text(t + "'");
            }
            else {
                window.location.href = "succ.html";
            }
        }, 1000);

    });
</script>
         <style type="text/css">
        .progress {
            position: absolute;
            z-index: 200;
            right: 20px;
            top: 20px;
        }

        .line {
            width: 13px;
            height: 140px;
            margin: 0 auto;
            background-color: #d6d6d6;
            border-radius: 20px;
        }

        .round {
            width: 60px;
            line-height: 60px;
            background-color: #2bc4c2;
            border-radius: 50%;
            font-weight: bold;
            color: #fff;
            margin-top: -5px;
        }

        .line-now {
            background-color: #2bc4c2;
            border-radius: 20px;
            /*height: 50%;*/
            bottom: 0px;
        }

        .font-nr {
            font-size: 15px;
            line-height: 45px;
        }
    </style>
 
       <div id="progressbar" class="progress">
            <ul class="line">
                <div id="linepercent" class="line-now"></div>
            </ul>
            <ul id="linetime" class="round font-nr">
                0'
            </ul>
        </div>

 

2.在某個操作后后面的代碼延遲執行 

設置一個延時來推遲執行隊列中之后的項目。

 delay(duration, [queueName])
duration (Integer) 以毫秒為單位的整數,用於設定隊列推遲執行的時間。
queueName (String) 可選參數,隊列名的字符串。默認是動畫隊列 fx 。
.delay() 用於將隊列中的函數延時執行。它既可以推遲動畫隊列中函數的執行,也可以用於自定義隊列。只有在隊列中的連續事件可以被延時,因此不帶參數的 .show() 和 .hide() 就不會有延時,因為他們沒有使用動畫隊列。

毫秒為單位的延時,數字越大,動畫越慢。字符串 'fast' 和 'slow' 分別代表200和600毫秒的延時。

例如,我們可以在 <div id="foo"> 的 .slideUp() 和 .fadeIn() 動畫之間添加800毫秒的延時 :

$('#foo').slideUp(300).delay(800).fadeIn(400);


當這句語句執行后,元素會有300毫秒的卷起動畫,接着暫停800毫秒,再出現400毫秒淡入動畫。

.delay() 用在jQuery動畫或者類似隊列中是再好不過的了。但是,由於其本身的限制,比如無法取消延時,所以不應完全用來替代 JavaScript 原生的 setTimeout 函數,后者更適用於通常情況。

3.setTimeout(function(){})

setTimeout()

   從載入后延遲指定的時間去執行一個表達式或者是函數;

      僅執行一次 ;和window.clearTimeout一起使用.
如果用這個函數,那么第一個進度條的例子可以改成下面的示例代碼,

        demoProcess();

        function demoProcess() {
            if (i < 100) {
                i = i + 5;
                t++;
                $("#linepercent").css("height", i + "%");
                $("#linetime").text(t + "'");
            }
            else {
                window.location.href = "succ.html";
            }

            setTimeout(demoProcess, 1000);
        }

以上都用了jquery類庫。




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM