jQuery根据当前剩余的第二次时间和基于PC时间的更新每分钟自动刷新 [英] jQuery auto refresh every minute based on current rest second time and based on PC time updating

查看:88
本文介绍了jQuery根据当前剩余的第二次时间和基于PC时间的更新每分钟自动刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继续此问题

我具有JS函数,每60秒通过ajax刷新一次.

I have JS function to refresh by ajax every 60 seconds.

var fetchData  = function()
{
    $.ajax(
    {
        url: "chkProfile.php",
        type: "POST",
        data:
        {
        },
        dataType: "JSON",
        success: function (jsonStr)
        {
        }
    });
}

fetchData();

var remainTime = 60 - parseInt((new Date().getTime() / 1000) % 60);
setTimeout(function()
{
    fetchData();

    setInterval(fetchData, 60000);
}, remainTime*1000);

此功能运行正常. 因此,该功能将基于剩余的秒数每60秒(1分钟)运行一次. 示例我在18:51:30刷新页面,然后剩下30秒了.遇到60秒后,请运行Ajax刷新.

This function is running OK. So, the function will run every 60 seconds(1 minute) based on rest of seconds. Example I refresh the page on 18:51:30 then We now the rest is 30 seconds to go. After meet 60 seconds then run the ajax refresh.

但是,当我尝试直接更改日期时间PC(Raspberry Pi 3)时遇到问题. 示例:我设置为18:53:40,那么我可以看到树莓派更新了.但是该功能每60秒出现一次错误.

But, facing problem when I try to change date time PC(Raspberry Pi 3) directly. Example: I set 18:53:40, then I can see the raspberry pc updated. But the function every 60 seconds to be incorrect.

我想要的是,无论我改变时间是什么,以便JS函数知道60秒有多少秒休息,达到60秒后再运行自动刷新功能.

What I want is, whatever I change the time so the JS function know how many seconds rest to be 60 seconds, after meet 60 seconds then run the auto refresh function.

已更新

检查后,我发现问题是

示例:

Time1: Current time: 18:30:15

然后我更改

Time2: To be: 18:30:01

该函数autorefresh仍然读取我更改的Time1而不是Time2. 因此,自动刷新仍会基于Time1刷新.

The function autorefresh still read the Time1 not Time2 that I changed. So auto refresh will refresh still based on Time1.

推荐答案

尝试这种方法:

var fetchData  = function(){
    $.ajax({
        url: "chkProfile.php",
        type: "POST",
        data:
        {
        },
        dataType: "JSON",
        success: function (jsonStr)
        {
        }
    });
}

fetchData();

setInterval(function(){
   var second = parseInt((new Date().getTime() / 1000) % 60);
    if(second === 0) {
       fetchData();
      }
},1000);

这篇关于jQuery根据当前剩余的第二次时间和基于PC时间的更新每分钟自动刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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