简体   繁体   中英

Ajax call every minute

I have a folder watcher that i want to be called once a minute, but i cant get it working.

The folder watcher will return 1 or 0 . If data == 1 then the page will be refreshed, if 0 wait a min and run again.

Can someone help me to find out whats wrong?

The script:

     <script type="text/javascript"> 
function timedRefresh(timeoutPeriod) {
setTimeout(Update(),timeoutPeriod);
}

function Update() {
            $.ajax({
            url: "checkfolder.php", 
            type: "POST",


            success: function (data) {

                if(data == "1"){
                   //Page will be updated
                }
                else{
                    timedRefresh(60000);
                }

            }
        });

        }

</script>

Heres the checkfolder.php:

    <?php
// Configuration ///////////////////////////////////////////////////////////////
$host ='xxxx';
$port = 21;
$user = 'xxxx';
$pass = 'xxxx';
$remote_dir = '../img/uploads/';
$cache_file = 'ftp_cache';

// Main Run Program ////////////////////////////////////////////////////////////

// Connect to FTP Host
$conn = ftp_connect($host, $port) or die("Could not connect to {$host}\n");

// Login
if(ftp_login($conn, $user, $pass)) {

  // Retrieve File List
  $files = ftp_nlist($conn, $remote_dir);

  // Filter out . and .. listings
  $ftpFiles = array();
  foreach($files as $file)
  {
    $thisFile = basename($file);
    if($thisFile != '.' && $thisFile != '..') {
      $ftpFiles[] = $thisFile;
    }
  }

  // Retrieve the current listing from the cache file
  $currentFiles = array();
  if(file_exists($cache_file))
  {
    // Read contents of file
    $handle = fopen($cache_file, "r");
    if($handle)
    {
      $contents = fread($handle, filesize($cache_file));
      fclose($handle);

      // Unserialize the contents
      $currentFiles = unserialize($contents);
    }
  }

  // Sort arrays before comparison
  sort($currentFiles, SORT_STRING);
  sort($ftpFiles, SORT_STRING);

  // Perform an array diff to see if there are changes
  $diff = array_diff($ftpFiles, $currentFiles);
  if(count($diff) > 0)
  {
    echo "1";//New file/deleted file
  }
  else{
   echo "0";//nothing new
}

  // Write new file list out to cache
  $handle = fopen($cache_file, "w");
  fwrite($handle, serialize($ftpFiles));
  fflush($handle);
  fclose($handle);
}
else {
  echo "Could not login to {$host}\n";
}

// Close Connection
ftp_close($conn);
?>

just change

setTimeout(Update(),timeoutPeriod);

to

setTimeout(Update,timeoutPeriod);

setTimeout takes the function reference as the first parameter while you were passing the function call. You dont need the setInterval here as on receiving '0' you are already calling the refresh function.

You need to pass function reference to setTimeout , also need to use setInterval() as you need to invoke it every minute

function timedRefresh(timeoutPeriod) {
    setInterval(Update,timeoutPeriod);
}

All you need to do is to put your function inside $(document).ready() and change your time out structure:

<script>
   $(document).ready(function(){
       setTimeout(function(){
         Update()
       },timeoutPeriod);
   });
</script>

Try this one

$(document).ready(function(){
    setInterval(function(){ 
        //code goes here that will be run every 5 seconds.    
        $.ajax({
            type: "POST",
            url: "php_file.php",
            success: function(result) {
                //alert(result);
            }
        });
    }, 5000);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM