首页 > 解决方案 > 如何在预设时间刷新代码的元素或特定部分?

问题描述

我目前有这段代码可以刷新整个 html 页面。在我的脚本中,我设法设置它,以便页面使用此函数在我的数据数组(由一系列时间值组成)中的每个时间值之前 10 分钟刷新。但是,这会重置我页面上的每个元素。有什么方法可以在预设时间(例如每天下午 12:50)刷新脚本中的特定元素,例如 div - 而不会干扰其他元素?

function refreshAt(hours, minutes, seconds) {
    var now = new Date();
    var then = new Date();

    if(now.getHours() > hours ||
        (now.getHours() == hours && now.getMinutes() > minutes) ||
        now.getHours() == hours && now.getMinutes() == minutes && now.getSeconds() >= seconds) {
            then.setDate(now.getDate() + 1);
    }
    then.setHours(hours);
    then.setMinutes(minutes);
    then.setSeconds(seconds);

    var timeout = (then.getTime() - now.getTime());
    setTimeout(function() { window.location.reload(true); }, timeout);
}

标签: javascripthtmlfunctionrefreshreload

解决方案


推荐阅读