轮播脚本中的 Jquery 函数定义
我有一个用于带有循环的轮播图像的脚本
$(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;
}
});
但我在 firebug 中收到此 javascript 错误:
无用的 setInterval 调用(参数周围缺少引号?) [Interrompi per questo errore] var run = setInterval(rotate(), speed);
我认为这是旋转函数定义的错误!
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;
}
});
But I receive this javascript error in firebug:
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这意味着你应该这样写:
而不是
因为你需要将函数的引用传递给 setInterval,你传递的是函数rotate()的返回值;
This means the you should write:
instead of
because you need to pass the reference of a function to setInterval, what you are passing is the return value of the function rotate();