我如何让这个 javascript 每秒运行一次?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5638783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:11:46  来源:igfitidea点击:

How do i get this javascript to run every second?

javascriptajax

提问by user663049

Sorry i'm abit of a noob I just want to know how i get this javascript to run every second?

抱歉,我是个菜鸟,我只想知道如何让这个 javascript 每秒运行一次?

source code:

源代码:

<script type="text/javascript">
$(function() {
    //More Button
    $('.more').live("click",function()  {
        var ID = $(this).attr("id");
        if(ID) {
            $("#more"+ID).html('<img src="moreajax.gif" />');

            $.ajax({
                type: "POST",
                url: "ajax_more.php",
                data: "lastmsg="+ ID, 
                cache: false,
                success: function(html){
                    $("ol#updates").prepend(html);
                    $("#more"+ID).remove();
                }
            });
        } else {
            $(".morebox").html('no posts to display');
        }

        return false;

    });
});

</script>

回答by Mike Lewis

Use setInterval()to run a piece of code every x milliseconds.

使用setInterval()每 x 毫秒运行一段代码。

You can wrap the code you want to run every second in a function called runFunction.

您可以将要每秒运行的代码封装在一个名为 的函数中runFunction

So it would be:

所以它会是:

var t=setInterval(runFunction,1000);

And to stop it, you can run:

要阻止它,您可以运行:

clearInterval(t);

回答by Town

Use setInterval:

使用setInterval

$(function(){
setInterval(oneSecondFunction, 1000);
});

function oneSecondFunction() {
// stuff you want to do every second
}

Here's an articleon the difference between setTimeoutand setInterval. Both will provide the functionality you need, they just require different implementations.

下面是文章上的区别setTimeoutsetInterval。两者都将提供您需要的功能,它们只需要不同的实现。

回答by Vismari

You can use setTimeout to run the function/command once or setInterval to run the function/command at specified intervals.

您可以使用 setTimeout 运行一次函数/命令或使用 setInterval 以指定的时间间隔运行函数/命令。

var a = setTimeout("alert('run just one time')",500);
var b = setInterval("alert('run each 3 seconds')",3000);

//To abort the interval you can use this:
clearInterval(b);

回答by Stephen P

Use setInterval(func, delay)to run the funcevery delaymilliseconds.

使用setInterval(func, delay)funcdelay毫秒运行一次。

setTimeout()runs your function onceafter delaymilliseconds -- it does not run it repeatedly. A common strategy is to run your code with setTimeout and call setTimeout again at the end of your code.

setTimeout()在几毫秒后运行一次你的函数delay——它不会重复运行它。一个常见的策略是使用 setTimeout 运行您的代码并在您的代码结束时再次调用 setTimeout。

回答by Christopher Armstrong

You can use setInterval:

您可以使用setInterval

var timer = setInterval( myFunction, 1000);

Just declare your function as myFunction or some other name, and then don't bind it to $('.more')'s live event.

只需将您的函数声明为 myFunction 或其他名称,然后不要将其绑定到$('.more')的实时事件。

回答by Nitin Midha

window.setTimeout(func,1000);

This will run func after 1000 milliseconds. So at the end of func you can call window.setTimeout again to go in a loop of 1 sec. You just need to define a terminate condition.

这将在 1000 毫秒后运行 func。因此,在 func 结束时,您可以再次调用 window.setTimeout 以进入 1 秒的循环。您只需要定义一个终止条件。

Reference

参考