<?php namespace PHPFUI\HTMLUnitTester; class Throttle { private float $lastAccessed = 0.0; private float $microseconds = 0.0; public function __construct(?int $microseconds = 0) { $this->lastAccessed = \microtime(true); if ($microseconds) { $this->microseconds = 1.0 / 1_000_000.0 * $microseconds; } } public function delay() : void { if ($this->microseconds) { $now = \microtime(true); $timeDifference = $now - $this->lastAccessed; if ($timeDifference < $this->microseconds) { \usleep((int)(($this->microseconds - $timeDifference) * 1_000_000.0)); } $this->lastAccessed = $now = \microtime(true); } } }