简体   繁体   English

setInterval无法使用回调函数

[英]setInterval not working with callback function

I am trying to use the setInterval to call a function that will load orders using ajax. 我正在尝试使用setInterval调用将使用ajax加载订单的函数。 However when I pass in a argument to the call back, the setInterval function stops working. 但是,当我将参数传递给回调时,setInterval函数将停止工作。 W w ^

hen I don't pass in any arguments it starts working . 如果我不传递任何论点,它就会开始工作。

Any ideas on how I can make it work with arguments? 关于如何使它与参数一起使用的任何想法? Below is my code 下面是我的代码

Thank you in advance! 先感谢您!

function httpGetRequest(page){
        $.get(page)
        .done(function(data){
              $(".display_orders").html(data);
        });
}
setInterval(httpGetRequest('load_orders.php'), 30000);

In this case 在这种情况下

setInterval (httpGetRequest('load_orders.php'), 30000);

the function executes immediately 该函数立即执行

Use like this 这样使用

setInterval ( function() { 
     httpGetRequest('load_orders.php')
 }, 30000);

Whenever you use setInterval or setTimeout be sure to give it a variable name so that you can cancel it if you need to. 每当使用setIntervalsetTimeout确保为其提供一个变量名,以便在需要时可以将其取消。

var orderInterval = setInterval(function(){httpGetRequest('load_orders.php');}, 30000);

In the event that you want to stop it you are now able to call: 如果您想停止它,现在可以调用:

clearInterval(orderInterval);

另一种方法是使用以下语法:

setInterval(httpGetRequest, 30000, 'load_orders.php');

Also you can call like this. 您也可以这样打电话。

function myFunction()
{
httpGetRequest('load_orders.php');
}

function httpGetRequest(page){

        $.get(page)

            .done(function(data){
                $(".display_orders").html(data);
            });


}

setInterval (myFunction, 30000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM