在n秒后不断重定向

Constantly redirecting after n seconds

本文关键字:重定向      更新时间:2023-09-26

我有一个url列表,我想在一个弹出说10秒打开。所以我点击一个按钮,它会打开第一个url,然后等待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>
<?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>

变化

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);

将是一个好的开始

我会这样做:

            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。你提到按钮点击。代码还检查是否没有更多的项目播放

这个问题的关键是,当你打开带有第一个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);
}