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

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.


1.php


div#load_here



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


1.php


2.php


3.php


4.php


5.php



Thanks in advance






You mean every 5s load all files or every 5s load one file and go to next file for next interval?

– Mohammad
Sep 15 '18 at 8:10






not all, every 5 seconds 1.php loaded, on next 5seconds 2.php loaded, and another 5 seconds 3.php loaded after last.php loaded I also want to back to 1.php

– Goon
Sep 15 '18 at 8:12




3 Answers
3



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>






Default value of index should be 1 because 0.php doesn't exist as in question mention

– Mohammad
Sep 15 '18 at 8:20


index


1


0.php






@Mohammad I am incrementing before I call the loadlink function so we should be ok

– cdoshi
Sep 15 '18 at 8:21






No, in first calling out of interval index is 0

– Mohammad
Sep 15 '18 at 8:22


index


0






Ahh, I see. Made the change

– cdoshi
Sep 15 '18 at 8:24






thank you very much it is work, I have made very small changes to make it loop, by changing clearInterval(timer); to index =1 :-)

– Goon
Sep 15 '18 at 8:41


clearInterval(timer);


index =1


<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 every5 sec and in last will call first php. also clear setInterval after call finished.


5 sec


first php


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



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)