How to fire AJAX request Periodically?

后端 未结 4 805
执笔经年
执笔经年 2020-11-22 14:02

This script reloads or refresh the page after every 5 seconds. But I want to do it using jQuery a

4条回答
  •  孤街浪徒
    2020-11-22 14:33

    Yes, you could use either the JavaScript setTimeout() method or setInterval() method to invoke the code that you would like to run. Here's how you might do it with setTimeout:

    function executeQuery() {
      $.ajax({
        url: 'url/path/here',
        success: function(data) {
          // do something with the return value here if you like
        }
      });
      setTimeout(executeQuery, 5000); // you could choose not to continue on failure...
    }
    
    $(document).ready(function() {
      // run the first time; all subsequent calls will take care of themselves
      setTimeout(executeQuery, 5000);
    });
    

提交回复
热议问题