57c63188f210c1729ea19d75dc2cbb7f42270064
[PHP-P10.git] / Tools / timer.inc.php
1 <?php
2 $timers['id']=0;
3
4 function timer_loop() {
5  global $timers;
6  $mtime = microtime(true);
7  $ret = false;
8  foreach ($timers as $id => $timer) {
9   if(($timer['expire'] - 0.00019) <= $mtime) { //we expire a timer 0,19 ms before (to reduce timer desyncs)
10    if((is_array($timer['function']) && method_exists($timer['function'][0],$timer['function'][1])) || (!is_array($timer['function']) && function_exists($timer['function']))) {
11     call_user_func_array($timer['function'],$timer['params']);
12    }
13    $ret = true;
14    unset($timers[$id]);
15   }
16  }
17  return $ret;
18 }
19
20 function timer($seconds,$command,$parameter) {
21  global $timers;
22  $new['expire'] = microtime(true) + $seconds;
23  $new['function'] = $command;
24  $new['params'] = $parameter;
25  while(isset($timers[$timers['id']])|| !isset($timers['id'])) {
26   $timers['id']++;
27   if($timers['id'] > 9999999) $timers['id'] = 0;
28  }
29  $timers[$timers['id']] = $new;
30  return $timers['id'];
31 }
32
33 function utimer($seconds,$command,$parameter) {
34  global $timers;
35  $new['expire'] = microtime(true) + ($seconds / 1000);
36  $new['function'] = $command;
37  $new['params'] = $parameter;
38  while($timers[$timers['id']] || !$timers['id']) {
39   $timers['id']++;
40   if($timers['id'] > 9999999) $timers['id'] = 0;
41  }
42  $timers[$timers['id']] = $new;
43  return $timers['id'];
44 }
45
46 function kill_timer($id) {
47  global $timers;
48  unset($timers[$id]);
49 }
50
51 $timers['id']=0;
52
53 ?>