简体   繁体   English

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

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

I have a piece of jQuery code and I need to execute when page load for first time, then I need to execute the same code after 30 minutes (less take this value as example) of course if page is open. 我有一段jQuery代码,我需要在第一次页面加载时执行,然后我需要在30分钟后执行相同的代码(少用这个值作为示例),当然如果页面打开。 I know how to execute code every 30 minutes by using this: 我知道如何使用这个每30分钟执行一次代码:

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

But which is the best approach to do this, I mean execute code first time page loads and then every 30 minutes, do I explain myself? 但是这是最好的方法,我的意思是执行代码第一次加载页面然后每30分钟,我自己解释一下吗?

Put your function into a name func: 将您的函数放入名称函数:

function blah() {
...
}

Then call it: 然后叫它:

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

or 要么

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

Or just 要不就

(function() {
    // Your code

    // Then call itself again
    setTimeout(arguments.callee, 18000000);
})()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM