n秒后不断重定向

时间:2014-03-22 06:18:23

标签: javascript php redirect time

我有一个网址列表,我想在弹出窗口打开10秒钟。所以我点击一个按钮,它将打开第一个网址,然后等待10秒,然后播放下一个,依此类推,直到它结束。 我找到了一些我认为可以工作或帮助的功能,我认为我的逻辑是正确的,并且认为它应该可行,但也许有更多知识的人可以帮助我。这就是我所拥有的:

<script type="text/javascript"> 
    function Redirect(url) {
        popupWindow = window.open(
        url,'popUpWindow','height=481,width=858,left=10,top=10,resizable=no,scrollbars=no,toolbar=no,menubar=no,location=no,directories=no,status=no')

    }

    function newPopup() {
        <?php 
        $jsSql = mysql_query("SELECT * FROM `songs`"); 
        while($jsRow = mysql_fetch_array($jsSql))
        {?>
            setTimeout('Redirect("<?php
            echo "http://www.youtube.com/embed".$jsRow['url']."?autoplay=1";?>")', 4000);
        <?php
        }
        ?>
    }
</script>

4 个答案:

答案 0 :(得分:1)

<?php
    $db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');
?>

<script type="text/javascript"> 
    function Redirect(url) {
        window.open(url, 'popUpWindow', 'height=481,width=858,left=10,top=10,resizable=no,scrollbars=no,toolbar=no,menubar=no,location=no,directories=no,status=no');
    }

    function newPopup() {
        <?php
            $stmt   = $db->query("SELECT * FROM `songs`");
            $songs  = $stmt->fetchAll(PDO::FETCH_OBJ);

            foreach($songs AS $index => $song) {
                printf("setTimeout(Redirect('http://www.youtube.com/embed%s?autoplay=1'), 4000);", $song->url);
            }
        ?>
    }

    // Start
    newPopup();
</script>

答案 1 :(得分:0)

更改

setTimeout('Redirect("<?php
  echo "http://www.youtube.com/embed".$jsRow['url']."?autoplay=1";?>")', 4000);

setTimeout(function() {
   Redirect("<?php
     echo "http://www.youtube.com/embed".$jsRow['url']."?autoplay=1";?>")}, 4000);

将是一个良好的开端

答案 2 :(得分:0)

我会这样做:

            var data = [];
            var current = 0;
            <?php 
            while($jsRow = mysql_fetch_array($jsSql))
                echo "data.push($jsRow['url']);";
            ?>

            function Redirect()
            {

            }

            function newPopup()
            {
                Redirect(data[current]);    
                current++;
                if (current < data.length)
                    setTimeout(function(){newPopup();}, 10*1000)    
            }

您所要做的就是在某些事件中第一次调用newPopup。你提到按钮点击。 该代码还会检查是否没有其他项目可以播放。

答案 3 :(得分:0)

此问题的关键在于,在使用第一个URL打开弹出窗口后,您只想在现有弹出窗口中设置window.location,以便它只加载新的URL。所以,它会是这样的:

// globals
var songList;

function openNewPopup(url) {
    return window.open(url, 'popUpWindow','height=481,width=858,left=10,top=10,
         resizable=no,scrollbars=no,toolbar=no,menubar=no,
         location=no,directories=no,status=no');
}

然后,对于后续页面加载到现有弹出窗口,您只需

function setNewPopupURL(url, popup) {
   popup.location = url;
}

我真的不懂PHP,但你想把歌曲列表放到一个JS变量中,以后可以循环:

// populate the songList
// the goal here is to do songList.push(songURL) for each song
// to add them all to the songList
<?php 
$jsSql = mysql_query("SELECT * FROM `songs`"); 
while($jsRow = mysql_fetch_array($jsSql))
{?>
    songList.push("<?php
    echo "http://www.youtube.com/embed".$jsRow['url']."?autoplay=1";?>");
<?php
}
?>

然后,你可以通过调用这样的函数来启动弹出旋转:

function runPopup() {
    var index = 0;
    var popup = openNewPopup(songList[index++]);

    function next() {
        setNewPopupURL(songList[index % songList.length), popup);
        ++index;
        setTimeout(next, 10*1000);
    }
    setTimeout(next, 10*1000);
}