tandefal/php-async

dev-main 2024-05-21 16:02 UTC

This package is auto-updated.

Last update: 2024-09-21 22:46:59 UTC


README

这是什么?

这是ReactPHP事件循环的辅助函数,ReactPHP是一个基于事件驱动、非阻塞I/O的PHP平台。这些定时器与JavaScript的setInterval、setTimeout和Promise类似。

安装

请确保已经安装了Composer

composer require tandefal/php-async

安装完成后,在您的代码中启用Composer的自动加载器

require 'vendor/autoload.php';

应用

  • interval参数以毫秒为单位
  • setInterval(callable $callback, int $interval): React\EventLoop\TimerInterface;
$count = 1;
setInterval(function () use(&$count) {
    echo "Count: {$count}\n";
    $count++;
}, 1000);
  • interval参数以毫秒为单位
  • setTimeout(callable $callback, int $interval): React\EventLoop\TimerInterface;
setTimeout(function () {
    echo "Hello World\n";
}, 1000);
  • clearTimeout(React\EventLoop\TimerInterface $timer): void;
$timeout = setTimeout(function(){
    //The following code will not run
    echo "Hello Planet\n";
}, 1000);
clearTimeout($timeout);
  • clearInterval(React\EventLoop\TimerInterface $timer): void;
setInterval(function($timer){
    clearInterval($timer);
    //The following code will only run once
    echo "Hello World\n";
}, 1000);

示例