开发者

jQuery.get queue the request until network connection available

开发者 https://www.devze.com 2023-04-01 15:51 出处:网络
I am using jQuery Mobile to create a webapp to look at and update a CRM type system. The mobile app sends update using jQuery.get and jQuery.post and they work fine when network connection is availab

I am using jQuery Mobile to create a webapp to look at and update a CRM type system.

The mobile app sends update using jQuery.get and jQuery.post and they work fine when network connection is available.

How should I code or what can I use to queue the jQuery.get and jQuery.post calls when the network connection is not available so they are sen开发者_如何转开发t when it becomes available again.


Edit: ah poo, i just noticed you said 'jQuery Mobile', I initially read that as jquery for mobile lol. Ummm, this'll probably only work as long as jQM supports ajax the same as normal jquery

I had an idea with a secondary ajax request, but you shouldn't need that. Just set up your AJAX like this, and give it a timeout. If it takes > 4 (should be enough for a broadband connection, but some phones may need ~10-15) seconds for the server to respond, it'll just try the ajax request again up to retryLimit, which can be set, then changed later as well after the 50 times is up (i.e. should it send when the program is idle and has no data perhaps?). When it connects, it'll go to the success function, which will then send the data to the server.

So it'd be like:

  $.ajax({
    type: 'GET',
    timeout: 4000,
    tryCount : 0,
    retryLimit: 50,
    success:function(data) {
            sendSavedData();
    }
    error: function(xhr, textStatus, errorThrown) {
      if(textStatus == 'timeout') {
          this.tryCount++;
          if(this.tryCount <= this.retryLimit) {
            $.ajax(this);
            return;
          }
          var check = confirm('We have tried ' + this.retryLimit + ' times to do this and the server has not responded. Do you want to try again?');
          if(check) {
            this.timeout = 200000;
            $.ajax(this);
            return;
          } else {
            return;
          }
      }
    }
  });
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号