MomasVII
MomasVII

Reputation: 5071

Constantly redirecting after n seconds

I have a list of urls that I would like to open in a popup for say 10 seconds. So I click a button and it will open the first url then wait 10 seconds and play the next and so on until it's over. I have found a few functions that I thought would work or help and I thought my logic was right and thought it should work but maybe someone with more knowledge can help me out. This is what I have:

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

Upvotes: 0

Views: 129

Answers (4)

jfriend00
jfriend00

Reputation: 707396

The key to this issue is that after you open the popup window with the first URL, you then want to just set the window.location on the existing popup window so that it just loads a new URL. So, it would be something like this:

// 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');
}

Then, for subsequent page loads into that existing popup window, you just

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

I don't really know PHP, but you'd want to put the list of songs into a JS variable that you can later loop over:

// 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
}
?>

And, then you can start the popup rotation by calling a function like this:

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

Upvotes: 0

Makla
Makla

Reputation: 10459

I would do it like this:

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

All you have to do is to call newPopup for the first time on some event. You mention button click. The code also check if there are no more items to play.

Upvotes: 0

Adrian Preuss
Adrian Preuss

Reputation: 3113

<?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>

Upvotes: 1

mplungjan
mplungjan

Reputation: 177975

Change

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

to

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

would be a good start

Upvotes: 0

Related Questions