轮播脚本中的jQuery函数定义 [英] Jquery Function definition in a Carousel Script

查看:107
本文介绍了轮播脚本中的jQuery函数定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于LOOP的轮播图片的脚本

I have this script for a Carousel Images with LOOP

$(document).ready(function() {

//rotation speed and timer
var speed = 5000;
var run = setInterval(rotate(), speed);   

//grab the width and calculate left value
var item_width = $('#slides li').outerWidth(); 
var left_value = item_width * (-1); 

//move the last item before first item, just in case user click prev button
$('#slides li:first').before($('#slides li:last'));

//set the default item to the correct position 
$('#slides ul').css({'left' : left_value});

//if user clicked on next button
function rotate() {
    //get the right position
        var left_indent = parseInt($('#slides ul').css('left')) - item_width;

        //slide the item
        $('#slides ul').animate({'left' : left_indent}, 3000, function () {

            //move the first item and put it as last item
            $('#slides li:last').after($('#slides li:first'));                  

            //set the default item to correct position
            $('#slides ul').css({'left' : left_value});

        });

        //cancel the link behavior
        return false;
}       

});

但是我在萤火虫中收到此javascript错误:

But I receive this javascript error in firebug:

无用的setInterval调用(在参数周围缺少引号吗?) [每个问题的错误间隔] var run = setInterval(rotate(),speed);

useless setInterval call (missing quotes around argument?) [Interrompi per questo errore] var run = setInterval(rotate(), speed);

我认为这是旋转函数定义的错误!

I thing it's an error of rotate function definition!

推荐答案

这意味着您应该写:

 var run = setInterval(rotate, speed);   

代替

 var run = setInterval(rotate(), speed);   

因为您需要将函数的引用传递给setInterval,所以传递的是函数Rotate()的返回值;

because you need to pass the reference of a function to setInterval, what you are passing is the return value of the function rotate();

这篇关于轮播脚本中的jQuery函数定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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