English 中文(简体)
无限休息时间等待2次,使服务器停电。
原标题:Infinite loop wait 2 second, make server call jquery

我正在玩笑,想呼吁提供因特网服务,即知道如何这样做,但服务器呼吁继续运行,直到从服务器得到具体答复。

我将等到每个周期的3秒钟。

function servercall() {
            while (true) {
                // code for clone and insert  ...
                $.ajax({
                    type: "POST",
                    url: "Server.asmx/HelloWorld",
                    data: "{ name :  " + $( #name ).val() + " ,  time :  2pm }",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(msg) {
                        AjaxSucceeded(msg);
                    },
                    error: AjaxFailed
                });
                setTimeout("nothing", 2000);
            }

        }
最佳回答

使用<条码>准时。 确实,当你收到答复时,就开始时间,以便你能够说明网络的拖延(你不想同时提出大量要求)。

function servercall() { 
  $.ajax({ 
    complete: function(xhr) { 
      var msg = xhr.responseText;
      if(xhr.statusCode == 200)
        AjaxSucceeded(msg); 
      else
        AjaxFailed(msg);

      setTimeout(servercall, 2000); //recursion magic
    }
  }); 
}
问题回答

http://www.w3schools.com/js/js_timing.asp”rel=“nofollow noretinger”>settimeout() Function。

你可以尝试:

function serverCall() {
    // ajax code

    setTimeout("serverCall()", 2000);
}

function ajaxSucceeded(msg) {
    if (isTheAnswerYouAreLookingFor) {
        // do stuff
    } else {
        setTimeout("serverCall()", 2000);
    }
}

Here is an article on setTimeout and setInterval that may help you further:
http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/

间隔时间更有助于:

<script type= text/javascript >

setInterval("servercall()", 2000);

function servercall() {

                // code for clone and insert  ...
                $.ajax({
                    type: "POST",
                    url: "Server.asmx/HelloWorld",
                    data: "{ name :  " + $( #name ).val() + " ,  time :  2pm }",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(msg) {
                        AjaxSucceeded(msg);
                    },
                    error: AjaxFailed
                });

            }

        }

</script>




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签