开发者

Timer using PHP

I want to execute a PHP function in every 5mins. A basic timer should do the trick. But how?

Example: I want to show something every 5mins in the scree开发者_Python百科n (web page) using PHP and ajax or just PHP alone.


In your case use javascript or meta timer - it will do the trick

Javascript

  setTimeout( 'yourAjaxFunction()', 3000 );


You need to use server-side Crontab utility if you don't want to execute it yourself all the time from the browser. So you either have to find this option somewhere in shared hosting panel settings, or manually set it up on server if you own it. You can either call it through PHP's command line mode, or use curl on wget to fetch via http. You also can set up how often you can call it.

For example command can look like

* * * * */5 /usr/local/bin/wget -q -O /dev/null http://example.com/auto.php


Use a javascript timer that will refresh every 5 minutes(or will send a ajax request). You cannot use PHP on the client side, use javascript.


You can do it with jQuery, i made a simple function to do this:

function Timer()
{
    var timeout = 30000;
    setTimeout(function() {
        $.ajax({
            url: "urltofile",
            type: "POST",
            success: function(response)
            {
                [....] // do stuff with the response
                Timer(); // set new instance for timer after the response
            }
        });
    }, timeout);
}
//
Timer();

And in a PHP file you can load stuf you want to show

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜