jQuery如何每5秒触发一次相同的事件 [英] jquery how to fire the same event every 5 seconds

查看:708
本文介绍了jQuery如何每5秒触发一次相同的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用左右滚动创建了一个简单的轮播.现在,我想每5秒自动滚动一次.这是我的代码:

I've built a simple carousel with left and right scroll. Now I want to scroll automatically every 5 seconds. Here's my code:

function carousel(){
        $j('#carousel_ul li:first').before($j('#carousel_ul li:last'));

        $j('#right_scroll img').click(function(){

            var item_width = $j('#carousel_ul li').outerWidth() + 10;

            var left_indent = parseInt($j('#carousel_ul').css('left')) - item_width;

            $j('#carousel_ul:not(:animated)').animate({'left' : left_indent},800, 'easeOutExpo',function(){

                $j('#carousel_ul li:last').after($j('#carousel_ul li:first'));

                $j('#carousel_ul').css({'left' : '-750px'});
            });
        });

        $j('#left_scroll img').click(function(){

            var item_width = $j('#carousel_ul li').outerWidth() + 10;

            var left_indent = parseInt($j('#carousel_ul').css('left')) + item_width;

            $j('#carousel_ul:not(:animated)').animate({'left' : left_indent},800, 'easeOutExpo',function(){

            $j('#carousel_ul li:first').before($j('#carousel_ul li:last'));

            $j('#carousel_ul').css({'left' : '-750px'});
            });

        });
}

我该如何实现? 在此先感谢:)

How do I achieve that? Thanks in advance :)

毛罗

推荐答案

您可以使用setInterval.看:

You can use setInterval. Look:

window.setInterval(event, 5000);

函数事件应该是

function event() {
 $j("#right_scroll img").click();
}


谢谢@cris! setTimeout仅调用一次.我更改为setInterval.

Tks @cris! setTimeout calls only one time. I changed to setInterval.

Tks @熊!现在,我将一个函数传递给setInterval.

Tks @bears! Now I pass a function to setInterval.

这篇关于jQuery如何每5秒触发一次相同的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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