在页面首次加载时执行 jQuery 代码,然后在一段时间后加载

Execute jQuery code when page loads for first time and then after time period

本文关键字:加载 代码 然后 一段时间 jQuery 执行      更新时间:2023-09-26

我有一段jQuery代码,我需要在第一次加载页面时执行,然后我需要在30分钟后执行相同的代码(少以这个值为例)当然如果页面是打开的。我知道如何使用它每 30 分钟执行一次代码:

window.setInterval(function() {
    ...
    });
}, 1800000); // Updates each 30 minutes

但是哪种是最好的方法,我的意思是第一次加载页面,然后每 30 分钟执行一次代码,我解释一下自己吗?

$(window).load(function () {
    function func() {
        //code here
    }
    func(); // call the function window.load 
    window.setInterval(func, 1800000); // also set setInterval to run function func .
});

$(document).ready(function () {
    function func() {
        //code here
    }
    func(); // call the function window.load 
    window.setInterval(func, 1800000); // also set setInterval to run function func .
});

把你的函数放到一个名称函数中:

function blah() {
...
}

然后称之为:

window.setInterval(blah, 18000000);
blah();

或者只是

(function() {
    // Your code
    // Then call itself again
    setTimeout(arguments.callee, 18000000);
})()