NotionCommotion Posted December 2, 2016 Share Posted December 2, 2016 After a React loop addPeriodicTimer timer is started, how do I change it? For instance, how can I change interval1 timer within the socket on data callback? Thanks <?php require 'vendor/autoload.php'; use React\EventLoop\Factory; use React\Socket\Connection; $interval1=20; $interval2=50; $loop = Factory::create(); $app = new \ClientApp; $server = @stream_socket_client("tcp://123.421.23.41:123", $errno, $errstr, STREAM_CLIENT_ASYNC_CONNECT); $socket = new Connection($server, $loop); $socket->on('data', function($data) use ($app) { //Change $interval1 }); $loop->addPeriodicTimer($interval1, function() use ($app) { $app->do_something(); }); $loop->addPeriodicTimer($interval2, function() use ($app) { $app->some_other_non_related_loop(); //Just shown so that you know there is more than one loop happening, so one must be able to somehow select the correct one when changing the timer }); Quote Link to comment https://forums.phpfreaks.com/topic/302667-change-react-loop-addperiodictimer-timer/ Share on other sites More sharing options...
kicken Posted December 3, 2016 Share Posted December 3, 2016 From digging around in the react code it looks like you just have to remove the timer then re-add it. $timer1 = $loop->addPeriodicTimer($interval1, function() use ($app) { $app->do_something(); }); $socket->on('data', function($data) use ($app, $loop, &$timer1) { $loop->cancelTimer($timer1); $newInterval = 10; $timer1 = $loop->addPeriodicTimer($newInterval, function() use ($app){ $app->do_something(); }); }); 1 Quote Link to comment https://forums.phpfreaks.com/topic/302667-change-react-loop-addperiodictimer-timer/#findComment-1539983 Share on other sites More sharing options...
NotionCommotion Posted December 3, 2016 Author Share Posted December 3, 2016 From digging around in the react code it looks like you just have to remove the timer then re-add it. Thanks kicken, When "digging around", where do you normally start? Does one normally start on the interface code? Quote Link to comment https://forums.phpfreaks.com/topic/302667-change-react-loop-addperiodictimer-timer/#findComment-1539998 Share on other sites More sharing options...
kicken Posted December 3, 2016 Share Posted December 3, 2016 When "digging around", where do you normally start? Does one normally start on the interface code? Depends a bit on what I'm looking for. In this case I was looking for a method to either re-register the timer or a way to change the interval of the timer so I started by just looking at the LoopInterface and TimerInterface. Neither of those have a way to change just the interval but the loop interface does have a cancel method. I didn't really spend time looking around at any implementations as I figured if there is no way on the official interface then there's probably just not a way at all. If I were more interested in knowing how something actually worked then I'd go directly to the implementation code rather than the interfaces. Quote Link to comment https://forums.phpfreaks.com/topic/302667-change-react-loop-addperiodictimer-timer/#findComment-1540006 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.