Search the Community
Showing results for tags 'setinterval'.
-
I have a very troubling problem at hand. I am using a web-socket server that runs in PHP. The issue is I need to be able to use a setInterval/setTimeout function similar to javascript, but within my php server. I do not have the time or resources to convert my entire project over to nodejs/javascript. It will take forever. I love php so much, that I do not want to make the switch. Everything else works fine and I feel like it's not worth it to re-write everything just because I cannot use a similar setInterval function inside php. Since the php socket server runs through the shell, I can use a setInterval type function using a loop: protected function on_server_tick($counter) { if($counter%5 == 0) { // 5 seconds interval } if($counter%10 == 0) { // 10 seconds interval } } $this->last_tick_time = microtime(true); $this->tick_interval = 1; $this->tick_counter = 0; while(true) { //loop code here... $t= microtime(true) - $this->last_tick_time; if($t>= $this->tick_interval) { $this->on_server_tick(++$this->tick_counter); $this->last_tick_time = microtime(true) - ($t- $this->tick_interval); } } This code does work as intended, but it seems a bit overboard for resources and I feel like that while loop will suck a lot resources. Is there anyway I can re-compile PHP from source and include a "while2" loop that only iterates every 500 miliseconds instead of instantly?