tags:

views:

329

answers:

4

Hello,

I am making a simple game with jquery and i want to make a call to asp.net web service which i know how to do, but the server call need to continue to run until i get a specific response from the server.

I will wait like 3 seconds with each loop cycle

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);
            }

        }
A: 

You want the JavaScript setTimeout() Function.

Drew Wills
A: 

You could try either:

function serverCall() {
    // ajax code

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

or:

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/

mynameiscoffey
A: 

Set interval is more helpful :

<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>
Zaje
What if they have slow internet and it takes a second for each response?
Josh Stodola
Its something one has to take into consideration if one has decided todo an infinite loop AJAX calls. He asked for 2 seconds i gave him two seconds.
Zaje
All I am saying is that this could potentially have multiple requests going at once, which is never desired.
Josh Stodola
The only thing that he can do is either drop the plan or increase the wait time to 10 seconds or more.
Zaje
+2  A: 

Use recursion with setTimeout. And be sure start the timer when you receive a response, so that you account for network delays (you don't want a bunch of requests going at the same time)...

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

      setTimeout(servercall, 2000); //recursion magic
    }
  }); 
}
Josh Stodola