How to make the code pause after completion

I’d like to force my code to pause after completion and before displaying a completion message. I’ve tried sleep and wait commands but they affect the start of running the code. I’ve searched the web for how to do this for two days with no success. Tried usleep() and the page wouldn’t even load. The reason for wanting the delay before showing the completion message is that the code processes so quickly that the completion message appears as soon as the page shows on the computer. I’d appreciate some help on this. Here’s my code:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0">
  <title>Foxclone backup, restore, and cloning utility</title>  
  <meta name="description" content= "FoxClone is a Linux based image backup, restore and clone tool using a simple point and click interface." />
  
    <link rel="stylesheet" type="text/css" media="screen" href="css/update.css"> 

    <!-- Favicon  -->
    <link rel="apple-touch-icon" sizes="180x180" href="images/favicon/apple-touch-icon.png">
    <link rel="icon" type="image/png" sizes="32x32" href="images/favicon/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="16x16" href="images/favicon/favicon-16x16.png">
    
    <link rel="mask-icon" href="images/favicon/safari-pinned-tab.svg" color="#5bbad5">
    <meta name="msapplication-TileColor" content="#da532c">
    <meta name="theme-color" content="#ffffff">

</head>
<body>
 
 <div class="container"> 
     <div class="row" style="text-align:center;" >
       <h1>Foxclone Filelist Updating</h1>
     </div>
     <div class="header">
 </div>   
<?php
 
  
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);

    $php_scripts = '../php/';
    require $php_scripts . 'PDO_Connection_Select.php';
    
    if (!$pdo = PDOConnect("foxclone_data"))     {	
        echo( 'Connection Failed');
    }
  
        $meg = 1048576 ;  // needed to convert filesize bytes to Mb
        $stmt = $pdo->prepare("UPDATE `files` SET `filename` = ?, `md5` = ?, `filesize` = ?,`logtime` = now() WHERE `id` = ?") ;
        $path = 'download/';  
        $id = NULL;
        $filename = '';

        foreach(glob($path.'*.*') as $file) {
          $id = NULL; 
          $filename = basename($file) ;
          $file = new SplFileInfo($file);
          $extension  = $file->getExtension(); 
          $test = substr($filename, 0,12);
            if ($test == 'foxclone_std') {
                $id = 1;
            }

          if (substr($filename, 0,9) == "foxcloneV"){
                $id = 2;
             }
                             
          if ($extension == "deb") {
                 $id = 3;
            }
                                   
          if ($extension == "gz") {
                   $id = 4 ;
            }

          if (substr($filename, 0,4) == "news") {
                   $id = 5 ; 
            }
                                     
          if (substr($filename, 0,13) == "foxclone_edge") {
                   $id =6;
            }
            $bytes = number_format(filesize($file) / 1048576, 2) ;
            $getdata = (array($filename, md5_file($file),$bytes , $id)) ;
                        
            $count = $stmt->execute ($getdata) ; 
               IF ($count == 0) {
                echo("Failed to update ".$file); 
                 die();
        }
     
    }
    
   
   ?>
<--Here's where I want the pause -->
<div class="row" style="text-align:center;" </div>   
    <h1>Database Update Successful</h1>

??? A PHP page is processed on the server. Then, it sends the combined HTML and outputs from PHP to the browser. There is now pausing! Perhaps you can explain what you mean about pausing!

The script processes so fast that the html completion message displays on the screen at the same time as the header text. I’d like to delay displaying the completion message for 5 seconds or so before displaying the completion message. I could throw a do…while loop at the end of the php to cause the delay, and an echo after it, but there must be a more elegant solution.

Again, PHP is server-side only! It can NOT work in any type of code on the browser! If you SLEEP() the code, it is done long before the browser ever sees it. Just can NOT be done the way you mind is thinking…
But, there is always a solution to all things code!

First, you can create PHP scripts and call them in a browser using AJAX calls. This way you can separate up scripts and delay them as you want. I don’t see any real reason for this, but, it can be done. Or, you can just delay it in the browser using simple JS code. There are simple JavaScript commands to force sleeping. Or you could use an on-page-load JS code to pop up an alert which would need clearing before showing the data.

Why would you want to delay a display? Normally, you would show a message and the resulting data below it. So, recapping, you can not delay SERVER-SIDE, meaning no delay in PHP. The browser is where you would delay results. Several ways to do that. One simple way would be to hide, meaning display:none the DIV that holds the results and use JS to time out after a set period and then show the DIV. Or, use AJAX calls to load the DIV with the data after the delay set time limit.

1 Like

Thanks for that explanation, ErnieAlex. I’ll investigate using JS code.

PHP approach is not your answer.

Javascript or jQuery is what you are looking for.

When your PHP is outputting the HTML content… have it it output some JS code that uses:

setTimeout()

So it can do whatever you like. at any given delay length/period.

Sponsor our Newsletter | Privacy Policy | Terms of Service