PHPackages                             babakbay/non-blocking-timer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. babakbay/non-blocking-timer

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

babakbay/non-blocking-timer
===========================

Non-blocking JavaScript-like setTimeout/setInterval for PHP.

00PHP

Since Nov 10Pushed 6mo agoCompare

[ Source](https://github.com/babakbay/non-blocking-timer)[ Packagist](https://packagist.org/packages/babakbay/non-blocking-timer)[ RSS](/packages/babakbay-non-blocking-timer/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

A PHP Non-blocking Timer Library
================================

[](#a-php-non-blocking-timer-library)

This timer uses a combination of a forked process (or a thread if Swoole v6+ is detected) together with POSIX signals to invoke a callback at a specified interval with millisecond-level accuracy — even when the PHP interpreter is suspended (e.g. during database or network queries, or system calls such as sleep(), curl\_exec(), fread(), etc).

Unlike timers implemented using event loops or coroutines, this approach does not depend on the PHP event loop being "active". The timer continues to run independently in its own process or thread, ensuring precise timing under blocking conditions.

This brings `setTimeout()` and `setInterval()` style functionality to PHP!

---

Requirements
------------

[](#requirements)

RequirementMinimum VersionPHP8.1+POSIX extensionEnabled`pcntl` extensionEnabled(Optional) `swoole`v6.0+ for thread backendOSLinux / macOS (not supported on Windows)---

Usage
-----

[](#usage)

### One-shot timeout (setTimeout)

[](#one-shot-timeout-settimeout)

```
use BabakBay\NonBlockingTimer\Timer;

$timer = new Timer();
$timer->setTimeout(function() {
    echo "Executed after 1 second\n";
}, 1000);

// Timer continues even during blocking operations
while (true) {
    sleep(2);
}
```

### Repeating interval (setInterval)

[](#repeating-interval-setinterval)

```
$timer = new Timer();
$counter = 0;

$timer->setInterval(function() use (&$counter) {
    echo "Tick " . ++$counter . "\n";
}, 500);

while (true) {
    sleep(2);
}
```

### Clear timer

[](#clear-timer)

To stop and clear an active timer, use the clear() method.

```
$timer->clear();
```

### Critical sections

[](#critical-sections)

You can disable callbacks during critical sections of your code using the callback() method.

```
$timer = new Timer();

$timer->setInterval(function() {
    echo "Timer callback\n";
}, 100);

// Disable callbacks during critical section. By default once callbacks
// are re-enabled, the callback will be invoked if there were any callback
// invokations blocked.

$timer->callbacks(false);
performCriticalOperation();
$timer->callbacks(true); // Re-enable ca;;nacls and invoke any pending callback

// If you do not want to invoke any pending callbacks, pass true as the
// second argument.

$timer->callbacks(false, true); // Do not pend callbacks
performCriticalOperation();
$timer->callbacks(true); // Re-enable callbacks
```

### Signal Masking

[](#signal-masking)

If you want to block timer signals from being received during a critical section of your code, use the signals() method. Any pending signals will be triggered once signals are re-enabled.

```
$timer = new Timer();
$timer->setInterval(function() {
    echo "Timer callback\n";
}, 100);

// Block signals during system call
$timer->signals(false);
$result = curl_exec($ch);
$timer->signals(true);
```

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance47

Moderate activity, may be stable

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity13

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/4f182db6aac3002e1a541a93a6ad6dbdb22c492b53d5cbe8a286ff620ed1c88e?d=identicon)[babakbay](/maintainers/babakbay)

---

Top Contributors

[![babakbay](https://avatars.githubusercontent.com/u/143432326?v=4)](https://github.com/babakbay "babakbay (6 commits)")

### Embed Badge

![Health badge](/badges/babakbay-non-blocking-timer/health.svg)

```
[![Health](https://phpackages.com/badges/babakbay-non-blocking-timer/health.svg)](https://phpackages.com/packages/babakbay-non-blocking-timer)
```

###  Alternatives

[davidgorges/human-name-parser

Parses a human name

19391.4k6](/packages/davidgorges-human-name-parser)[friendsoftypo3/kickstarter

Extension Kickstarter - Kickstart TYPO3 Extension

243.1k](/packages/friendsoftypo3-kickstarter)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
