AJAX jQuery - update content every minute

I think of a clock that will be updated every minute if the system clock on the user's computer looks like this, for example. 11:08:00, it will be updated and 11:09:00, etc.

I tried setInterval():

setInterval(function(){
    $.ajax({
        url: "../time.php",
        context: document.body,
        success: function(result){
            $('.time').html(result);
        }
    })
}, 60000);

But it reloads every minute after the page loads.

Is there any solution?

+3
source share
6 answers

Try this code:

var interval    =   0;
function start(){
    setTimeout( function(){
        ajax_function();
        interval    =   60;
        setInterval( function(){
            ajax_function();
        }, interval * 1000);
    }, interval * 1000);    
}

function ajax_function(){
    $.ajax({
        url: "../time.php",
        context: document.body,
        success: function(result){
            $('.time').html(result);
        }
    }); 
}

$( window ).load(function(){
    var time    =   new Date();
    interval    =   60 - time.getSeconds();
    if(interval==60)
        interval    =   0;
    start();
});
+3
source

setInterval()will not immediately call its function, the first time the function is launched, in this case it will be 60 seconds after the page loads. You can make the following changes to get the result you are looking for:

  • Put AJAX request in function
  • Call the function as soon as the page loads
  • setTimeout() .ajax(), setInterval() . , setInterval() , setTimeout() , .
  • , , , .time, . , result 11:08:00
  • , , ,
function update(){
    $.ajax({
        url: "../time.php",
        context: document.body,
        success: function(result){
            var secondless = result.split(':'), $time = $('.time');
            secondless = secondless [0]+':'+secondless [1];
            $time.html(function(_,v){
                return secondless != v ? secondless : v
            }
            setTimeout(update, 1000);
        }
    })
}
update();
+4
var d = new Date();
var n = d.getSeconds(); 
while(n != 0){
    d = new Date();
    n = d.getSeconds(); 
    if(n == 0){
         setInterval(function(){
           $.ajax({
               url: "../time.php",
               context: document.body,
               success: function(result){
                    $('.time').html(result);
                }
             })
         }, 60000);
    }
} 
+2

, Date time getSeconds(), 0, , , 60, setTimeout, setInterval .

;

var time = new Date();
var timeout = 0;

if ( time.getSeconds() != 0 ) {
   timeout = (60 - time.getSeconds());
}

setTimeout(function(){

    setInterval( function(){
    $.ajax({
      url: "../time.php",
      context: document.body,
      success: function(result){
        $('.time').html(result);
      }
    })}, 60000);

}, timeout);

;

setTimeout(function(){

    setInterval( function(){
    $.ajax({
      url: "../time.php",
      context: document.body,
      success: function(result){
        $('.time').html(result);
      }
    })}, 60000);

}, (60 - new Date().getSeconds()) );
+2

user2703250 , , :

var d = new Date();
var n = d.getSeconds(); 
while(n !== 0){
d = new Date();
n = d.getSeconds(); 
if(n === 0){ dopage_update();}
}

function dopage_update(){
     $.ajax({
            url: "../time.php",
            context: document.body,
            success: function(result){
                  $('.time').html(result);
                  setTimeout(function(){dopage_update();}, 60000);
            }
          });
         }
+1

, seTimeout setInterval , hh:mm:00.

0
source

All Articles