2013-10-06 67 views
-1

試圖尋找這樣的東西,只是找不到真正的即時消息後很抱歉,如果這已經回答了某個地方。運行代碼一段時間

我需要運行一段代碼一段時間,不是在之後的一段時間。 基本上我想在頁面上快速顯示一個數組的隨機值,我希望這樣可以持續顯示1分鐘然後停止。

下面的代碼將只在3秒後開始,並不停止,我不知道我怎麼能實現這一點,所以任何幫助非常感謝。

var messages = ["Good!", "Great!", "Awesome!", "Super!", "Nice!"]; 
    function getMessage() { 
     return messages[Math.floor(Math.random() * messages.length)]; 
    } 
    setTimeout(function() { oneSecondFunction(); }, 3000); 
    function oneSecondFunction() { 
     $('#test').html(getMessage()); 
     setTimeout('oneSecondFunction()', 100); 
    } 

感謝

+0

你是什麼意思,停止運行?示例代碼修改了dom並完成了。 – user1937198

+0

@ user1937198 - 不,它修改dom,_schedules本身在100ms以後_,並結束。 –

回答

1

嘗試

var messages = ["Good!", "Great!", "Awesome!", "Super!", "Nice!"]; 

function getMessage() { 
    return messages[Math.floor(Math.random() * messages.length)]; 
} 

var interval = null; 
setTimeout(function() { 
    interval = setInterval(function() { 
     // Your code here 
     $("#test").html(getMessage()); 
    }, 100); 

    //Stop the functions after 1 minute. 
    setTimeout(function() { clearInterval(interval); }, 60 * 1000); 
}, 3000); 

這在3秒後將創建一個間隔,這將執行的代碼每隔100ms 1分鐘。

+0

感謝您也! – user1925633

+0

+1,可能是最適合JavaScript的解決方案。您可以將'interval'的聲明以及第二個'setTimeout'調用(清除間隔)移動到第一個'setTimeout'函數中。然後你有一個正確的獨立封閉(並且在稍後的超時時間內不需要額外的'+ 3000')。附: OP希望更新持續一分鐘而不是三分鐘。 –

+0

感謝您的改進建議,我編輯瞭解決方案。我認爲我已經搞亂了我腦海中的3秒... –

1

設置結束時間設置一個標誌,只有重新安排oneSecondFunction如果標誌未設置第二超時:

var messages = ["Good!", "Great!", "Awesome!", "Super!", "Nice!"]; 
var stop = false; 
function getMessage() { 
    return messages[Math.floor(Math.random() * messages.length)]; 
} 
setTimeout(function() { 
    setTimeout(function() { stop = true; }, 60000); // one minute later 
    oneSecondFunction(); 
}, 3000); 
function oneSecondFunction() { 
    $('#test').html(getMessage()); 
    if (!stop) { 
     setTimeout('oneSecondFunction()', 100); 
    } 
} 
+0

謝謝磨坊!像魅力 – user1925633

1

跟蹤多少時間已經過去了自第一次運行該功能以來,並且該時間段超過一分鐘時,請不要更新setTimeout呼叫。

例如:

var timeStarted; 

function getMessage() { 
    return messages[Math.floor(Math.random() * messages.length)]; 
} 

setTimeout(function() { oneSecondFunction(); }, 3000); 

function oneSecondFunction() { 
    var now = Date.now(); 
    timeStarted = timeStarted || now; 

    $('#test').html(getMessage()); 

    if (now - timeStarted < 60000) { 
     setTimeout(oneSecondFunction, 100); // you can just write function's name 
    } 
} 
1

您只需要跟蹤函數運行的時間。然後你測試一下時間是否結束。這是你如何做到的一個例子。

var messages = ["Good!", "Great!", "Awesome!", "Super!", "Nice!"], 
    interval = 100, 
    delay = 0; 

    function getMessage() { 
     return messages[Math.floor(Math.random() * messages.length)]; 
    } 
    setTimeout(oneSecondFunction, 3000); // Less code is better. 

    function oneSecondFunction() { 
     $('#test').html(getMessage()); 
     delay+= interval; 
     if (delay < (3 * 60 * 1000)) { // 3 minutes 
      setTimeout(oneSecondFunction, interval); 
     } 
    } 
0

您不應該從oneSecondFunction設置Timeout來實現循環。相反,使用setInterval函數並傳遞你想要調用的函數,以及想要睡多久。然後用間隔ID調用clearInterval來停止它。像這樣:

function getMessage() { 
return messages[Math.floor(Math.random() * messages.length)]; 
} 

function oneSecondFunction() { 
$("#test").html(getMessage()); 
} 
var intervalID = setInterval(oneSecondFunction, <Delay between runs in millis here>); 
function stop() { 
clearInterval(intervalID); 
} 
setTimeout(stop, 60000); 
+0

這是有缺陷的,因爲它不會等待所需的三秒鐘,然後開始100毫秒的時間間隔。否則,這與[Simon Fischer的回答](http://stackoverflow.com/a/19212164/535871)有何不同? –