简体   繁体   中英

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

I have a javascript code like this :

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.

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? 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 . also clear setInterval after call finished.

$(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++;
     };
});

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