Skip to content
PHP Logo

Are there specific technologies or methods recommended for displaying a spinner in PHP for tasks that take a significant amount of time?

When a task in PHP takes a significant amount of time to complete, it is recommended to use asynchronous processing or AJAX to display a spinner or loading indicator to keep the user informed about the progress of the task. This can prevent the user from experiencing a long period of waiting without any feedback.

php
<!-- HTML -->
<div id="spinner" style="display:none;">
    <img src="spinner.gif" alt="Loading..." />
</div>

<script>
// JavaScript
function showSpinner() {
    document.getElementById('spinner').style.display = 'block';
}

function hideSpinner() {
    document.getElementById('spinner').style.display = 'none';
}

// AJAX request example
showSpinner();
$.ajax({
    url: 'long_running_task.php',
    success: function(response) {
        // Handle the response
        hideSpinner();
    }
});
</script>