JQuery simple slider issue on timeOut
I have a function called slideMe()
and I need to launch that with delay of some seconds for each slider links.
jquery version is 1.4.2
code is:
$(document).ready(function(){ var img_id; var clicked; function slideMe(img_id){ $('.slider-menu-item').css('color','#fff'); $('#'+img_id).css('color','#EBE1B9'); $('.slider-img').hide(); $('#slider-img-'+img_id).fadeIn(600); }; $.each($('.slider-menu-item'),function(){ img_id = $(this).attr("id"); setTimeout("slideMe()",900); console.log(img_id) }); $('.slider-menu-item').live('click',function(){ img_id = $(this).attr("id"); clicked = 1; slideMe(img_id); }); });//end document.ready
but in console logs I receive error that says:
slideMe is not defined
How I can do?
You need to define the slideMe
function outside of the document.ready handler. Also it would be better to pass directly the method to setTimeout instead of using strings. Also the slideMe
method expects an argument so make sure you pass it:
function slideMe(img_id) {
$('.slider-menu-item').css('color', '#fff');
$('#' + img_id).css('color', '#EBE1B9');
$('.slider-img').hide();
$('#slider-img-' + img_id).fadeIn(600);
}
$(document).ready(function() {
var img_id;
var clicked;
$('.slider-menu-item').each(function() {
img_id = $(this).attr('id');
setTimeout(function() {
slideMe(img_id);
}, 900);
});
$('.slider-menu-item').live('click',function() {
img_id = $(this).attr("id");
clicked = 1;
slideMe(img_id);
});
});
尝试slideMe=function(){...}
而不是function slideMe(){...}
上一篇: 点击跟踪旋转图像轮播的链接