最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ajax call every minute - Stack Overflow

programmeradmin0浏览0评论

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 parison
  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);
?>

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 parison
  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);
?>
Share Improve this question edited Apr 21, 2015 at 6:58 Benjamin 2,2961 gold badge17 silver badges24 bronze badges asked Apr 21, 2015 at 6:42 Jesper LundgrenJesper Lundgren 1151 gold badge2 silver badges10 bronze badges 1
  • 1 What isn't working as expected? – moffeltje Commented Apr 21, 2015 at 6:44
Add a ment  | 

4 Answers 4

Reset to default 5

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);
});
发布评论

评论列表(0)

  1. 暂无评论