常量脚本->更改src iframe(1分钟,5分钟)+ jQuery [英] Constant script -> change src iframe (1min, 5min) + jQuery

查看:113
本文介绍了常量脚本->更改src iframe(1分钟,5分钟)+ jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用jQuery编写脚本时遇到问题.

I'm having problems writing a script in jQuery.

我的页面中有一个iFrame,需要更改.
iframe的来源必须为http://www.example1.com持续1分钟,然后切换为http://www.example2.com持续5分钟.这是恒定循环.但是我该怎么办呢?

I have an iFrame in my page that needs to change.
The source of the iframe has to be http://www.example1.com for ONE minute and then switched to http://www.example2.com for FIVE minutes. This in a constant loop. But how can I do this?

我现在有:

jQuery(document).ready(function () {
    setTimeout(function() {
        if($('#iframe').attr('src') == "http://www.example1.com")
        {
            $('#iframe').attr('src',"http://www.example2.com");
        }
        else
        {
            $('#iframe').attr('src',"http://www.example1.com");
        }
    }, 10000);
});

但这并没有做太多.而且只运行一次..

But this doesn't do so much. And it only runs once ..

推荐答案

我相信这会起作用.每次调用其中一个功能时,都会为另一个功能设置新的超时时间.您最初显示1,然后设置1分钟超时.当该超时到期时,将显示2,并且将新的超时设置为5分钟,此时,将再次显示1.

I believe this will work. Each time one of the functions is called, it sets a new timeout for the other function. You initally show 1, then set a 1 minute timeout. When that timeout expires, 2 is shown and a new timeout is set for 5 minutes, at which point, 1 will be shown again.

function show1() { 
    iframe.attr('src', 'http://www.example1.com');
    setTimeout(function() { show2(); }, 1000 * 60);
}
function show2() { 
    iframe.attr('src', 'http://www.example2.com');
    setTimeout(function() { show1(); }, 1000 * 60 * 5);
}

jQuery(document).ready(function() {
    show1();
});

这篇关于常量脚本->更改src iframe(1分钟,5分钟)+ jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆