Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 阿贾克斯每分钟都打电话_Javascript_Php_Ajax - Fatal编程技术网

Javascript 阿贾克斯每分钟都打电话

Javascript 阿贾克斯每分钟都打电话,javascript,php,ajax,Javascript,Php,Ajax,我有一个文件夹监视程序,我想每分钟被呼叫一次,但我无法让它工作 文件夹监视程序将返回1或0。如果data==1则页面将被刷新,如果0等待一分钟并再次运行 有人能帮我找出毛病吗 剧本: <script type="text/javascript"> function timedRefresh(timeoutPeriod) { setTimeout(Update(),timeoutPeriod); } function Update() { $.aj

我有一个文件夹监视程序,我想每分钟被呼叫一次,但我无法让它工作

文件夹监视程序将返回
1
0
。如果
data==1
则页面将被刷新,如果
0
等待一分钟并再次运行

有人能帮我找出毛病吗

剧本:

     <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>

函数timedRefresh(timeoutPeriod){
setTimeout(Update(),timeoutPeriod);
}
函数更新(){
$.ajax({
url:“checkfolder.php”,
类型:“POST”,
成功:功能(数据){
如果(数据==“1”){
//页面将被更新
}
否则{
timedRefresh(60000);
}
}
});
}
下面是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);
?>

您只需将函数放入
$(document).ready()
中并更改超时结构:

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

$(文档).ready(函数(){
setTimeout(函数(){
更新()
},时间输出周期);
});

您只需将函数放入
$(document).ready()
中并更改超时结构:

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

$(文档).ready(函数(){
setTimeout(函数(){
更新()
},时间输出周期);
});

您需要将函数引用传递给,还需要使用,因为您需要每分钟调用一次

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

您需要将函数引用传递给,还需要使用,因为您需要每分钟调用它

function timedRefresh(timeoutPeriod) {
    setInterval(Update,timeoutPeriod);
}
换衣服

setTimeout(Update(),timeoutPeriod)

setTimeout(更新,timeoutPeriod)

setTimeout
在传递函数调用时将函数引用作为第一个参数。这里不需要设置间隔,因为在接收到“0”时,您已经在调用刷新功能。

只需更改即可

setTimeout(Update(),timeoutPeriod)

setTimeout(更新,timeoutPeriod)

setTimeout
在传递函数调用时将函数引用作为第一个参数。此处不需要设置间隔
,因为在接收到“0”时,您已经在调用刷新函数

试试这个

试试这个


什么不按预期工作?什么不按预期工作?