简体   繁体   English

每X分钟调用并运行Ajax

[英]Call and Run Ajax every X minutes

How call and run Ajax every X minutes without the page being open (client-side), running in the background on the server side. 如何每隔X分钟调用和运行Ajax,而无需打开页面(客户端),而是在服务器端的后台运行。

My Code in Header.php: 我在Header.php中的代码:

(function($) {
    function updateGo() {
        $(window).load(function() {
            $.ajax({
                url: '/test.com/template/test/script.php',
                dataType: 'html',
                cache: false,
                success: function(data) {
                    if (data) {
                        $('body').append(data);
                    }
                }
            });
        })
    }
    updateGo();
    setTimeout(updateGo, 60 * 1000);
})(jQuery);

In script.php there are some functions and codes jquery inside and it works perfectly, but only works when I enter the page (client-side), in case I am trying to make the functions and codes of this script always run without having to enter the page. script.php中 ,内部有一些函数和代码jquery,它可以完美运行,但仅当我进入页面(客户端)时才起作用,以防万一我试图使该脚本的功能和代码始终运行而不必进入页面。

If you want to Call and Run Ajax every X minutes jQuery you can try the function setInterval doc like: 如果您想每隔X分钟调用jQuery并运行Ajax,可以尝试使用setInterval doc函数,例如:

 (function($) { function updateGo() { $(window).load(function() { $.ajax({ url: '/test.com/template/test/script.php', dataType: 'html', cache: false, success: function(data) { if (data) { $('body').append(data); } } }); }) } updateGo(); // for this example we take 3000 milliseconds let interval = 3000; setInterval(updateGo, interval); })(jQuery); 

so it's work when you load your page in your browser. 因此,当您在浏览器中加载页面时,它就可以工作。

NB: in the background on the server side, we can't use Ajax, you must try to add cronJob little documentation about 注意:在服务器端的后台,我们不能使用Ajax,您必须尝试添加有关以下内容的 cronJob 小文档

I hope it's help you 希望对您有帮助

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

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