在 setInterval 函数中更改函数参数

发布于 2024-10-31 14:48:58 字数 604 浏览 0 评论 0原文

有没有办法将这五个 setTimeouts 放入一个 setInterval 中?我需要在每个时间间隔后以某种方式切换函数的参数。基本上,我希望能够清除动画而不必清除 5 个 setTimeouts。这是动画。 www.hodaradesign.com。谢谢!

<script type="text/javascript">
$(window).load(function () {
    setTimeout ("pulse('1')", 300); 
    setTimeout ("pulse('2')", 500); 
    setTimeout ("pulse('3')", 700); 
    setTimeout ("pulse('4')", 900); 
    setTimeout ("pulse('5')", 1100); 
});

function pulse(n) { 
    $(".roll"+n).animate({"opacity": "0"}, 650);
    setTimeout (function (){
        $(".roll"+n).animate({"opacity": "1"}, 350);
    },800)
};

</script>

Is there a way to put those five setTimeouts into one setInterval? I need to somehow switch the parameter of the function after each interval. Basically, I want to be able to clear the animation without having to clear 5 setTimeouts. Here is the anim. www.hodaradesign.com. thanks!

<script type="text/javascript">
$(window).load(function () {
    setTimeout ("pulse('1')", 300); 
    setTimeout ("pulse('2')", 500); 
    setTimeout ("pulse('3')", 700); 
    setTimeout ("pulse('4')", 900); 
    setTimeout ("pulse('5')", 1100); 
});

function pulse(n) { 
    $(".roll"+n).animate({"opacity": "0"}, 650);
    setTimeout (function (){
        $(".roll"+n).animate({"opacity": "1"}, 350);
    },800)
};

</script>

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

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

发布评论

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

评论(3

要走干脆点 2024-11-07 14:48:59

向脚本添加另一个参数来指定当前超时,然后使用函数本身来增加计数并确定何时(以及是否)发生下一次调用。

<script type="text/javascript">
$(window).load(function () {
    setTimeout( function() { pulse(1,200); }, 300 ); 
});

function pulse(n,time) { 
    if (n <= 5) {
       setTimeout( function() { pulse(n+1,time); }, time );
    }

    $(".roll"+n).animate({"opacity": "0"}, 650);
    setTimeout (function (){
        $(".roll"+n).animate({"opacity": "1"}, 350);
    },800);

};

</script>

Add another parameter to the script to specify the current timeout, then use the function itself to increment the count and determine when (and if) the next invocation occurs.

<script type="text/javascript">
$(window).load(function () {
    setTimeout( function() { pulse(1,200); }, 300 ); 
});

function pulse(n,time) { 
    if (n <= 5) {
       setTimeout( function() { pulse(n+1,time); }, time );
    }

    $(".roll"+n).animate({"opacity": "0"}, 650);
    setTimeout (function (){
        $(".roll"+n).animate({"opacity": "1"}, 350);
    },800);

};

</script>
︶ ̄淡然 2024-11-07 14:48:58

试试这个:

<script type="text/javascript">
$(window).load(function () {
        var iCounter = 1; //variable to keep track of current iteration.
        var interValKey = null;//variable to store the key to clear the interval later.
        setTimeout (function(){
            interValKey = setInterval(function(){
                pulse(iCounter); 
                iCounter++;
                if(iCounter == 6){
                clearInterval(interValKey);
                }
            }, 200);
        }, 300); 
});

function pulse(n) { 
    $(".roll"+n).animate({"opacity": "0"}, 650);
    setTimeout (function (){
        $(".roll"+n).animate({"opacity": "1"}, 350);
    },800)
};

</script>

Try this:

<script type="text/javascript">
$(window).load(function () {
        var iCounter = 1; //variable to keep track of current iteration.
        var interValKey = null;//variable to store the key to clear the interval later.
        setTimeout (function(){
            interValKey = setInterval(function(){
                pulse(iCounter); 
                iCounter++;
                if(iCounter == 6){
                clearInterval(interValKey);
                }
            }, 200);
        }, 300); 
});

function pulse(n) { 
    $(".roll"+n).animate({"opacity": "0"}, 650);
    setTimeout (function (){
        $(".roll"+n).animate({"opacity": "1"}, 350);
    },800)
};

</script>
沒落の蓅哖 2024-11-07 14:48:58

使用 setInterval 调用和函数都可以访问的变量。

<script type="text/javascript">
var n = 1;

$(window).load(function () {
    setTimeout(function() {
        setInterval(pulse, 200);
    }, 300);
});

function pulse() {
    var current_n = n; // avoid a race condition
    $(".roll" + current_n).animate({"opacity": "0"}, 650);
    setTimeout (function (){
        $(".roll" + current_n).animate({"opacity": "1"}, 350);
    }, 800);

    n += 1;
};

</script>

Use a variable that both the setInterval call and the function can access.

<script type="text/javascript">
var n = 1;

$(window).load(function () {
    setTimeout(function() {
        setInterval(pulse, 200);
    }, 300);
});

function pulse() {
    var current_n = n; // avoid a race condition
    $(".roll" + current_n).animate({"opacity": "0"}, 650);
    setTimeout (function (){
        $(".roll" + current_n).animate({"opacity": "1"}, 350);
    }, 800);

    n += 1;
};

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