2013-10-06 64 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)有何不同? –