douqianxun8540
2014-03-10 13:47
浏览 36
已采纳

PHP显示url并重定向到下一个循环

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.

图片转代码服务由CSDN问答提供 功能建议

我有数组 [1,2,55,76,...] < / p>

我在哪里生成网址

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

问题:如何显示每个网址几秒钟(3秒)和 重定向到下一个网址?

我的想法:使用iframe / jQuery

我愿意接受想法/代码/建议。 \ n

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongluxin2452 2014-03-10 14:06
    已采纳

    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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douxing7101 2014-03-10 14:15

    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

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题