简体   繁体   English

如何使用javascript每分钟间隔加载不同的页面?

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

I have a javascript code like this : 我有这样的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);

As you see this script will load 1.php in div#load_here every 1 minute. 如您所见,此脚本每1分钟1.phpdiv#load_here加载1.php

My concern is currently I have more than 1 php files (lets called them 1.php , 2.php , 3.php , 4.php , 5.php , etc.) and I want them to load consecutively every 1 minute? 我关心的是当前我有1个以上的php文件(叫它们1.php2.php3.php4.php5.php等),我希望它们每1分钟连续加载一次吗? I have no idea to do this 我不知道要这样做

Thanks in advance 提前致谢

You can do something like 你可以做类似的事情

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

calling link after every 5 sec and in last will call first php . 5 sec调用first php链接,最后调用first php also clear setInterval after call finished. 调用完成后还要清除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.

相关问题 Rails使用清单文件在不同页面中加载不同的javascript文件 - Rails load different javascript files in different pages using manifest file 使用javascript从不同的div加载来自不同域的多个页面 - load multiple pages from different domain in different divs using javascript 如何使用javascript创建具有当前时间和下一个10分钟间隔时间的数组? - How to create an array with current time and next 10 five minute interval times using javascript? 如何在Javascript中保持每分钟更新日期时间? - How to keep updating datetime every minute in Javascript? 如何在 Javascript 中每分钟比较 2 个日期变量? - How to compare 2 date variables every minute in Javascript? 如何使用javascript中的按钮加载HTML页面 - How load HTML Pages, using button in javascript 一天中的每一分钟都可以显示不同图像的JavaScript - A javascript that can display a different image, every minute of the day Javascript:将计时器设置为每分钟运行不同的链接 - Javascript: set the timer to run different link every minute 如何在Javascript中每次重新加载页面时每分钟附加到一个数组? - How to append to an array every minute on every reload of page in Javascript? 如何使用 JavaScript 加载不同的 html 页面,由用户触发,单击按钮? - How to load different html pages with JavaScript, triggered by the user, clicking on a button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM