Jump to content

PHP Repeating Interval Function


Alfred

Recommended Posts

Hey,

 

I'm not that familiar with PHP so maybe what I'm asking for is not even possible. I plan to establish a webserver that is connected to a RS232 device. I don't have any problems accessing  the RS232 interface with a PHP-script, it's all working fine.

 

Now I want the incoming data from the device being display on the servers webpage almost in realtime. When I say almost I mean that the client who is logged on to the website should be able to define the update rate of the incoming data.

 

So here comes the problem, I need something like the java script setinterval() function that is working on the server-side and repeats the data request to the device in an infinite loop with the given time interval.

I don't want to refresh the whole page as I will have to update about 20 sets of data within seconds!

 

I found this code:

http://phpclasses.chimit.nl/package/5544-PHP-Call-a-function-after-a-period-of-time.html

it looks pretty promissing but I just can't make it working...

 

As I said I'm a PHP-noob so I just put the code below on the start of my own code and provided the Timer.class.php file in my project folder. Could anyone explain how I could make the test function repeating endlessly on a 1sec basis using this code so I would get a screen full of "called"? So far I only get two "called" :-)

 

Thanks!

 

declare(ticks=100);
function test ()
{
    print "<br>called";
}
require_once 'Timers.class.php';
setTimeout('test', 110000000);
setInterval('test', 10000000);

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/219476-php-repeating-interval-function/
Share on other sites

what if you made a recursive function and had it sleep for x number of seconds before calling itself again.

 

<?php
function do_stuff(){

  // do something

sleep(20); // wait 20 seconds
do_stuff(); // call this function again
}
?>

@ micah1701:

Does this really work? So the script would sleep for 20 seconds but would I still be able to us all buttons while the script is in sleep mode? How about this timer solution is this feasible at all?

 

@doni49:

There would never be more than one client logged on, but you are right it's a risk. Maybe I somehow could inhibit further page access while someone is already logged on...

well... I kind of solved the problem. I created an iframe on an external php page that refreshes every X seconds without refreshing the whole main page... this is not perfect but a, acceptable workaround

 

this goes into main php file:

 

...
<iframe src='action.php' height='30' width='200'></iframe>
...

 

this goes into  the external php file:

<html>
<head><meta http-equiv="refresh" content="30; URL=action.php" /></head>
<body>
<?php
...
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.