Hi,
Today while testing my PRISM plugin, I noticed that sometimes 1 of my 2 timers stops randomly.
After investigating the issue I came to a conclusion. When the timer is created it adds it to an array of timestamps sorted by timestamps themselves. If a timestamp is being added, and another times with the same timestamp exists in the timers array, it is replaced and stops running.
Solution (can be probably neatened up but I suck at PHP):
Basically if the timestamp already exists, it adds a further "wait" of 0.01 seconds to make sure it doesn't replace the other timer.
After using this createTimer function, the problem goes away!
I was thinking of posting this to GitHub, but since my fix isn't so neat I am posting it here.
Today while testing my PRISM plugin, I noticed that sometimes 1 of my 2 timers stops randomly.
After investigating the issue I came to a conclusion. When the timer is created it adds it to an array of timestamps sorted by timestamps themselves. If a timestamp is being added, and another times with the same timestamp exists in the timers array, it is replaced and stops running.
Solution (can be probably neatened up but I suck at PHP):
<?php
// Registers a callback method.
protected function createTimer($callback, $interval = 1.0, $flags = Timer::CLOSE, $args = array())
{
# This will be the time when this timer is to trigger
$mtime = microtime(TRUE);
$timestamp = $mtime + $interval;
# Check to make sure that another timer with same timestamp doesn't exist
if (isset($this->timers["$timestamp"]))
{
$timestamp = $mtime + $interval + 0.01;
}
# Adds our timer to the array.
$this->timers["$timestamp"] = new Timer($this, $callback, $interval, $flags, $args);
}
?>
After using this createTimer function, the problem goes away!
I was thinking of posting this to GitHub, but since my fix isn't so neat I am posting it here.