0

I have array [1, 2, 55, 76, ...]

where I generate urls

www.example.com/id/1
www.example.com/id/2
www.example.com/id/55
...

Question: How to show each url for few second (3s) and redirect to next url?

My idea: use of iframe / jQuery

I am open to ideas/code/suggestions.

8
  • is javascript an option? Use a timer. You could write a function and use .delay() to hold up the script before redirecting to the next location. Commented Mar 10, 2014 at 13:49
  • @TonyMancini sleep is used on the server side, he can't show the code before sleep then redirect Commented Mar 10, 2014 at 13:49
  • Yeah I realized afterwards... deleted the comment and asked about JS instead. Commented Mar 10, 2014 at 13:50
  • @TonyMancini JS/jQuery is possible too Commented Mar 10, 2014 at 13:51
  • how many element are there in the array and can you send them all to js then process them in browser side or it has to be requested each time for the next url? Commented Mar 10, 2014 at 13:52

2 Answers 2

1

Here's a JS solution. All URIs are written into an array by PHP, then a JS interval cycles through them:

var urls = ['www.example.com/id/1','www.example.com/id/2','www.example.com/id/55'];

var current = 0;
var interval = window.setInterval(function(){
    document.getElementById('container').innerHTML = urls[current];
    current += 1;
        if(!urls[current]) {
        window.clearInterval(interval);
    }
}, 1000);

On the PHP side the first line will look like this ($uris is an array with all URIs):

echo "var urls = ['".implode("','", $uris)."']";

Here's a JSFiddle example.

Sign up to request clarification or add additional context in comments.

1 Comment

perhaps echo 'var urls = ', json_encode($urls); would be preferable, as would using a closure variable referencing the iframe be, and actually setting the iframe's src attribute to the new url
1

a pretty easy JS implementation would be this:

var interval = (function(iframe)
{
    var idx = 0,//current idx
        urls = <?= json_encode($urlArray); ?>;//array of urls
    iframe.src = urls[idx++];//set first url
    return setInterval(function()
    {//callback function, will be executed every 3 seconds
        iframe.src = urls[idx++];
        if (idx >= urls.length)
        {//we've just reached set last url
            idx = 0;//this resets the loop, after 3 seconds, the first url is used again
            //alternatively
            clearInterval(interval);//end of the loop
        }
    }, 3000);//3000 miliseconds, or every 3 seconds
}(document.getElementById('yourIframeID')));//pass reference to your iframe here

This line:

<?= json_encode($urlArray) ?>

Is, of course a PHP statement, and has to run server-side, if you can't alter the JS from PHP, you can get the urls through an ajax call

Fiddle example

The fiddle loops through the urls twice, and then just terminates the interval, the iframe.src statements have been commented out, since the fiddle uses a div, but you get the basic idea

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.