I
I
Ilya Korablev2015-11-08 11:51:58
JavaScript
Ilya Korablev, 2015-11-08 11:51:58

How to do a background page refresh?

How can I make it so that when the page is open, the reload.php page is updated in the background every 2 seconds, and when it is updated, .loader is shown?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Deodatuss, 2015-11-08
@Deodatuss

function reload( delta ) {
  var delta = delta || 0;
  setTimeout(function(){
    $('.loader').show();
    var start = newDate().getTime();
    $.ajax(url : "reload.php", success : function(data){
         ....data manipulation
         $('.loader').hide();
         var end = newDate().getTime();
         reload( end - start );
     });
  },2000 - delta);
});
reload();

W
wellgo, 2015-11-09
@wellgo

var uTimeout = false, uRequest = false, uFunction = function(){
   if (uRequest !== false) {
      uRequest.abort();
   }
   if (uTimeout !== false) {
      clearTimeout(uTimeout);
   }
   uTimeout = setTimeout(function(){
      uRequest = $.get('reload.php', {}, function(q){
         /* действия с DOM */
         uRequest = false;
         uTimeout = false;
         uFunction();
      });
   }, 2000);
};
uFunction();

1. If the response is JSON, then replace $.get with $.getJSON
2. If you need to pass something - "{}" to an object, you can generally remove it from the parameters.
3. If a POST request, then instead of $.get - $.post respectively
4. If you need a force update (for example, on a button click), just call uFunction()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question