php - JavaScript - How to switch interval between sites befor they are loaded
one text
Solution:
You could change from setInterval
to setTimeout
, creating a new timeout based on the 'next' site in question.
Something along these lines:
var anzeige;
var inhalt = ["site.php", "site1.php", "site2.php", "site3.php", "site4.php"];
var interval = [20000, 40000, 3000, 20000, 60000];// num elements match inhalt array
var position = 0;
var doNext;
$(document).ready(function () {
anzeige = $("#anzeige");
loadNext();
});
function loadNext() {
anzeige.load( inhalt[position] );
position++;
if (position >= inhalt.length) {
position = 0;
}
doNext = window.setTimeout(function(){ loadNext(); }, interval[position] );
}
You could combine both inhalt
and interval
to a single javascript object if it makes it easier to rationalize it. However above I simply matched the same number of array items to each so position
just referenced the right ones.
Edit: If you want the interval element to related to the current view page (as in, site1.php should be visible for 40 seconds, instead of 3 seconds before showing site2.php), then move the doNext = window.setTimeout
above the position++
line in the code. Just depends how you want to define those numbers. Time to next site, or time for current site ;)
You can see the above in action with this example page: http://bytejunkies.com/test/4266.php
(inspect source to see the intervals I set for those, to watch the network tab as well)
Source