jquery polling with smart poll plugin

2019-04-02 10:06发布

I'm trying for the life of me to get this plugin to work but I'm not understanding the status function so retry is not firing.

$.poll(10000, function(retry){
  $.get('willfail', function(response, status){
    if (status == 'success') {
      // Do something
      alert("YES");
    } else {
      alert("NO");
      //retry();
    }
  })
})

If I set the get request to '/' it will give me the alert YES message, but as it is, the alert No message never gets fired despite the ELSE.

I'm using a jquery polling plugin:

https://github.com/jeremyw/jquery-smart-poll

Any ideas?

2条回答
男人必须洒脱
2楼-- · 2019-04-02 10:26

That is probably a bad example. The callback passed to $.get will only be fired if the request succeeded. Try this:

$.poll(10000, function(retry){
  $.ajax({
      url:'willfail',
      success: function(){
          // Do something
          alert("YES");
      },
      error: function() {
          alert("NO");
          retry();
      }
  });
});

For more information, have a look at $.ajax.

查看更多
Fickle 薄情
3楼-- · 2019-04-02 10:36

You can also use Smartupdater - jQuery plugin

http://www.eslinstructor.net/smartupdater/

which let you:

  • Stop/Restart polling
  • Change polling timeout dynamically
  • Switch polling URL dynamically
  • Switch callback function dynamically
  • Remotely set polling timeout
  • Remotely select callback function
查看更多
登录 后发表回答