AJAX jQuery - 每分钟更新一次内容 [英] AJAX jQuery - update content every full minute

查看:92
本文介绍了AJAX jQuery - 每分钟更新一次内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户计算机上的系统时钟看起来如此,我正在考虑一个每分钟刷新一次的时钟。 11:08:00,它会刷新和11:09:00等。

I'm thinking on a clock that will refresh every full minute if system clock on users computer will look like this eg. 11:08:00, it'll refresh and 11:09:00 etc.

我已经尝试过 setInterval()

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

但它在页面加载后每分钟重新加载。

But it's reloading every minute after page load.

有什么解决方案吗?

推荐答案

试试这段代码:

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();
});

这篇关于AJAX jQuery - 每分钟更新一次内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆