简体   繁体   English

重新运行带有$ ajax对象的ajax调用?

[英]Re-run an ajax call with $ajax object?

I'm a little confused about this and I hope some can help explain this to me. 我对此有些困惑,希望有人能帮我解释一下。 Using jquery $ajax I run something like this (the values here are not relevant): 使用jquery $ ajax我运行类似的东西(这里的值不相关):

$ajax = $.ajax({
    type: type,
    url: url,
    data: data,
    dataType: dataType, 
    success: callBack,
    cache: cache,
    error:function (xhr, ajaxOptions, thrownError) {
        dir(thrownError);
        dir(xhr);
        dir(ajaxOptions);
    }
  });

How can I run this every few seconds? 如何每隔几秒钟运行一次? Should I just wrap it in setInterval() or can I use $ajax.done and pass in the ajax call again recursively? 我应该将其包装在setInterval()中还是可以使用$ ajax.done并递归地再次传递ajax调用? I'm not necessarily asking what the best way is just an example of how I could do this. 我并不一定要问最好的方法只是我如何做到这一点的一个例子。

You can use setInterval: 您可以使用setInterval:

setInterval(function(){
      $ajax = $.ajax({
    type: type,
    url: url,
    data: data,
    dataType: dataType, 
    success: callBack,
    cache: cache,
    error:function (xhr, ajaxOptions, thrownError) {
        dir(thrownError);
        dir(xhr);
        dir(ajaxOptions);
    }
  });
},1000);

Please take a look at web sockets , it is better than ajax if you want it to be called periodically 请看一下Web套接字 ,如果要定期调用它,它比ajax更好

setInterval setInterval

var call = function(){
   $.ajax({
    type: type,
   url: url,
   data: data,
   dataType: dataType, 
   success: callBack,
   cache: cache,
   error:function (xhr, ajaxOptions, thrownError) {
        dir(thrownError);
       dir(xhr);
       dir(ajaxOptions);
    }
}

and

   setInterval(call, 5000);

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

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