Hi all

I'd like to know how/if you can pause a loop for a few seconds and let it start again from where it stopped.

Dani AI

Generated

A quick addition to what already showed: pausing a loop on the server is possible, but its suitability depends on the context. Blocking delays are fine for CLI scripts or background batch processes. Inside a web request they block the HTTP response, tie up a PHP worker and can hit PHP's execution time limit (default 30 seconds), which makes them a poor fit for interactive pages on shared hosts.

A common, robust pattern for web UIs is to push the wait to the client: have a lightweight server endpoint perform a single step and return status, and have the browser schedule the next call after a short delay. That keeps each HTTP request short and lets progress be shown to the user. Example client loop:

let step = 0;

function runStep() {
  fetch('/step.php?step=' + step)
    .then(res => res.json())
    .then(data => {
      // update UI with data.progress
      step++;
      if (!data.done) setTimeout(runStep, 2000); // wait 2 seconds
    });
}

runStep();

For long-running work that must run entirely server-side, use a queue or background worker (cron, a daemon, Gearman/Beanstalkd/Redis, etc.) or run the script from CLI with appropriate execution limits. Streaming progress with output flushing is possible but often unreliable under FastCGI/nginx without special configuration, so prefer explicit polling or background job patterns for production use.

Recommended Answers

All 2 Replies

Thanks for that. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.