Job polling
This panel polls a simulated job until PHP reports completion. It includes pause, resume, and restart controls.
Complete installation first. Copy the files below to the indicated application locations.
No database or custom JavaScript is required. Each poll advances this simulated job by 25%; a real application would read its authorized job status instead.
Files
php
<?php
declare(strict_types=1);
namespace App\Island;
use Interact\Attribute\Action;
use Interact\Attribute\State;
use Interact\Island;
/**
* Simulates a finite job so polling can stop when PHP reports completion.
*
* Each refresh advances only this island's simulated state; no background worker or database
* is involved. A real application would read an authorized job's current status instead.
*/
class JobProgressIsland extends Island
{
#[State]
public int $progress = 0;
#[State]
public bool $running = true;
/**
* Advance the simulated job by one step and expose when polling should stop requesting.
*/
#[Action]
public function refreshStatus(): void
{
$this->progress = min(100, $this->progress + 25);
$this->running = $this->progress < 100;
}
/**
* Start the demonstration again through an explicit user action.
*/
#[Action]
public function restart(): void
{
$this->progress = 0;
$this->running = true;
}
}php
<?php
declare(strict_types=1);
/**
* templates/island/job_progress.php
*
* @var int $progress
* @var bool $running
*/
?>
<section
x-data="{ autoRefresh: true }"
x-interact:poll.2s.visible="autoRefresh && $server.state.running && $server.call('refreshStatus')"
>
<progress max="100" value="<?= $progress ?>" aria-label="Job progress"></progress>
<p role="status"><?= $running ? 'Running' : 'Completed' ?>: <?= $progress ?>%</p>
<button type="button" @click="autoRefresh = !autoRefresh" x-text="autoRefresh ? 'Pause' : 'Resume'"></button>
<button type="button" @click="$server.call('restart')">Restart job</button>
<p role="alert" x-text="$server.error ? $server.error.message : ''"></p>
</section>php
<?= $this->Interact->render('JobProgress') ?>Behavior
The visible panel requests status every two seconds while autoRefresh and running are true. Completion sets PHP running to false and stops further requests. Resuming the local preference cannot restart a completed job; the explicit restart action does that.
x-interact:poll documents visibility, request timing, and when edits or errors cause ticks to be skipped.