簡體   English   中英

如何使用javascript每分鍾間隔加載不同的頁面?

[英]How to load different pages in interval every minute using javascript?

我有這樣的JavaScript代碼:

function loadlink() {
    $('#load_here').load('1.php', function () {
        $(this).unwrap().addClass('scale-in');
    });
}

loadlink(); // This will run on page load
setInterval(function () {
    loadlink() // this will run after every 5 seconds
}, 60000);

如您所見,此腳本每1分鍾1.phpdiv#load_here加載1.php

我關心的是當前我有1個以上的php文件(叫它們1.php2.php3.php4.php5.php等),我希望它們每1分鍾連續加載一次嗎? 我不知道要這樣做

提前致謝

你可以做類似的事情

<script>
  var index = 1;
  function loadlink() {

    $('#load_here').load(index + '.php', function () {
      $(this).unwrap().addClass('scale-in');
    });
  }

  loadlink(); // This will run on page load
  var timer = setInterval(function () {
    index++;
    loadlink() // this will run after every 1 minute
    if(index == 5) {
       clearInterval(timer);
    }
  }, 60000);
</script>
<script>
  var files=['1.php','2.php','3.php']//etc
  function loadlink(file) {

    $('#load_here').load(file, function () {
      $(this).unwrap().addClass('scale-in');
    });
  }

  loadlink(files[0]); // This will run on page load
  setInterval(function () {
    var nextFile=files.shift();
    loadlink(nextFile) // this will run after every 5 seconds
    files.push(nextFile);
  }, 60000);
</script>

5 sec調用first php鏈接,最后調用first php 調用完成后還要清除setInterval

$(document).ready(function(){
    var arr = ['php1','php2','php3','php4','php5'], i = 0;    

    function loadlink(link) {
        console.log('calling link : ', link);
        $('#load_here').load(link, function () {
            $(this).unwrap().addClass('scale-in');
        });
    }


    var intervalId = setInterval(callFileLink, 60000);

     function callFileLink() {
        var link = arr[i];
        console.log("Message to alert every 5 seconds"+ link);
        if(link) {
            loadlink(link);
        }else {
          clearInterval(intervalId);
          loadlink(arr[0]);
        }
        i++;
     };
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM