簡體   English   中英

與jQuery一起運行while循環而不會使網站崩潰

[英]running a while loop alongside jQuery without crashing the website

我需要一個不斷運行的while循環來檢查很多if (value < otherValue || value === otherValue)而不會導致網站崩潰,並且仍然能夠接受點擊事件,等等。這是我的一些代碼:

$(document).ready(function(){
    var value = 0;
    var otherValue = 25;
    var otherValue2 = 75;
    var otherValue3 = 100;
    var otherValue4 = 125;
    var otherValue5 = 175;
    var otherValue6 = 200;
    var otherValue7 = 300;
    $('#button').click(function(){
        value += 5;
        // Here i make the value show on the page like fancy, but It's not essential for this question
    });
});

那么,如何包含一個不斷運行的while循環,使該代碼仍然起作用,並且不會使網站崩潰?

我將看一下setInterval它將使您能夠在每個設置的時間段內執行一個函數。

 var interval = null; var value = 0; var otherValue = 25; var intervalTimer = 100; // 100ms $('input').val(value); $('button').on('click', function(){ interval = setInterval(function(){ $('input').val(value += 5); }, intervalTimer); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input> <button> Start Updating Value </button> 

暫無
暫無

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

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