如何迭代数组中的 url,并在 iframe 中显示每个 url?

发布于 2024-11-06 01:14:38 字数 1100 浏览 0 评论 0原文

此代码的目的是循环遍历 URL 并将最新的 URL 插入 iframe 中。下面,我使用jquery的倒计时插件。

<?php
$count=0;
$urls[]="http://www.techcrunch.com";
$urls[]="http://www.livingsocial.com";
$urls[]="http://www.guardian.co.uk";
$urls[]="http://www.google.com";
$urls[]="http://www.rightmove.com";
$urls[]="http://www.godaddy.co.uk";

foreach ($urls as $count){

?>
<script type="text/javascript"> 
    $(function start() {
    var changetimer = new Date();
    changetimer.setSeconds(changetimer.getSeconds() + 10.5); 
    $('#defaultCountdown').countdown({until: changetimer, onExpiry: liftOff});
    $('#year').text(changetimer.getFullYear());
});

function liftOff() { 
    document.getElementById("mainview").src = '<?php echo($count); ?>';
} 
</script> 
<?php }  ?>
</head> 

下面是在 body 标签中。

<iframe id="mainview" width="800" height="400" src="http://www.holidays.com">
</iframe>

这里的问题是这段代码跳过了 url 列表,速度太快,您只能看到第一个和最后一个 url。到目前为止,中间的网址是不可见的,并且倒计时器仅出现一次。我正在使用 jquery 倒计时插件。

如何使其慢下来并在进入下一个之前单独显示倒计时的每次迭代?非常感谢。

The purpose of this code is to loop through the urls and insert the latest one into the iframe. Below, I am using jquery's countdown plugin.

<?php
$count=0;
$urls[]="http://www.techcrunch.com";
$urls[]="http://www.livingsocial.com";
$urls[]="http://www.guardian.co.uk";
$urls[]="http://www.google.com";
$urls[]="http://www.rightmove.com";
$urls[]="http://www.godaddy.co.uk";

foreach ($urls as $count){

?>
<script type="text/javascript"> 
    $(function start() {
    var changetimer = new Date();
    changetimer.setSeconds(changetimer.getSeconds() + 10.5); 
    $('#defaultCountdown').countdown({until: changetimer, onExpiry: liftOff});
    $('#year').text(changetimer.getFullYear());
});

function liftOff() { 
    document.getElementById("mainview").src = '<?php echo($count); ?>';
} 
</script> 
<?php }  ?>
</head> 

the below is in the body tag

<iframe id="mainview" width="800" height="400" src="http://www.holidays.com">
</iframe>

The problem here is this code skips through the list of urls so quickly you only see the first one, and the last url. The urls inbetween are so far invisible and the countdown timer only appears once. I am using the jquery countdown plugin.

How do I make this slow down and show each iteration of the count down individually before moving to the next? thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青朷 2024-11-13 01:14:38

问题是您将所有计时器设置为完全相同的时间。您需要将它们设置为不同的时间。与其为每个添加 10.5 秒,不如添加“10.5*数组索引”,以便每个等待比前一个等待的时间长一点。

The problem is that you are setting all of your timers to go off at exactly the same time. You need to set them for different times. Instead of adding 10.5 seconds for each one, maybe add "10.5*the array index" so that each one waits a little longer than the previous.

不爱素颜 2024-11-13 01:14:38

这样做:

<?php
$count=0;
$urls[]="http://www.techcrunch.com";
$urls[]="http://www.livingsocial.com";
$urls[]="http://www.guardian.co.uk";
$urls[]="http://www.google.com";
$urls[]="http://www.rightmove.com";
$urls[]="http://www.godaddy.co.uk";
?>

<script type="text/javascript"> 
$(function start() {
    var changetimer = new Date();
    changetimer.setSeconds(changetimer.getSeconds() + 10.5); 
    var urls = <?php echo json_encode($urls); ?>;
    $.each(urls, function(i, url){
       setTimeout(function(){
          liftOff(url);
       } ,5000*i); //Change every 5 seconds
    });
    $('#year').text(changetimer.getFullYear());
});

function liftOff(url) { 
    document.getElementById("mainview").src = url;
} 
</script> 

Do it like this:

<?php
$count=0;
$urls[]="http://www.techcrunch.com";
$urls[]="http://www.livingsocial.com";
$urls[]="http://www.guardian.co.uk";
$urls[]="http://www.google.com";
$urls[]="http://www.rightmove.com";
$urls[]="http://www.godaddy.co.uk";
?>

<script type="text/javascript"> 
$(function start() {
    var changetimer = new Date();
    changetimer.setSeconds(changetimer.getSeconds() + 10.5); 
    var urls = <?php echo json_encode($urls); ?>;
    $.each(urls, function(i, url){
       setTimeout(function(){
          liftOff(url);
       } ,5000*i); //Change every 5 seconds
    });
    $('#year').text(changetimer.getFullYear());
});

function liftOff(url) { 
    document.getElementById("mainview").src = url;
} 
</script> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文