函数中的javascript函数不工作?
编辑:新的问题是,即使当前的幻灯片。showCurrentSlide();函数位于hidePrevSlide函数内,在hidePrevSlide中的动画完成之前,正在执行showCurrentSLide代码 我正在尝试创建一个网站,如果你点击一个函数中的javascript函数不工作?,javascript,jquery,function,anonymous-function,Javascript,Jquery,Function,Anonymous Function,编辑:新的问题是,即使当前的幻灯片。showCurrentSlide();函数位于hidePrevSlide函数内,在hidePrevSlide中的动画完成之前,正在执行showCurrentSLide代码 我正在尝试创建一个网站,如果你点击一个,那么将得到一个类添加到其中。然后,它将向上滑动当前可见屏幕并将其隐藏,然后显示与对应的屏幕(例如,如果为“主页”,则它将向上滑动当前现有屏幕并将其隐藏,然后它将显示“#主页滑动”屏幕)。这是我的密码 $(document).ready(function
$(document).ready(function(){
hideItems(); //this function basically hides all the screens / slides.. The words 'screen' and 'slides' are interchangeable for the time being.
$('#homeSlide').fadeIn(1000); //the default screen to be shown is the home screen
$('#homeSlide').addClass('current'); //to signify that this is the current visible screen
$('#home').addClass('clicked'); //#home is the <li>, so it adds the .clicked class to the <li>
$('#sidebar ul a li').click(function(){ //loops through all <li>'s in the sidebar if an <li> is clicked
var current_slide = $(this);
$('#sidebar ul .clicked').removeClass('clicked'); // when an <li> is clicked, remove .clicked class from any other <li>'s
current_slide.addClass('clicked'); // add .clicked class to the clicked <li> ($(this))
hidePrevSlide(function(){
alert('enter showing step');
current_slide.showCurrentSlide();
});
});
});
现在,当我运行代码时,它会说“完成隐藏步骤”。现在应该进入显示步骤,但它没有说“进入显示步骤”,因此它没有进入应该在hidePrevSlide函数完成后执行的步骤。为什么?hidePrevSlide需要调用回调函数
function hidePrevSlide(){
var test = $('.current').attr('id');
test = "#" + test; // surrounds the id with a # and the word 'Slide'. This is the id of the screen which should slideUp
$(test).slideUp( function () {
$(test).hide();
$(test).removeClass('current');
});
alert('finished hiding step. Should enter showing step now');
};
function hidePrevSlide(callback){
var test = $('.current');
test.slideUp( function () {
test.hide();
test.removeClass('current');
});
alert('finished hiding step. Should enter showing step now');
callback();
};
我还删除了
$(test)
的用法。如果您有一个元素,则无需继续按ID搜索它。hideprevsiled
从不调用作为参数传递的函数。等待,for callback();我到底应该放什么?因为放置回调();不起作用,我还尝试放置当前幻灯片。showCurrentSlide();在放置回调()的位置;但这也不起作用动画是异步的。因此,应该等待动画完成的任何内容都应该在传递给test.slideUp
的函数中。您将函数hideprevsile()
更改为函数hideprevsile(callback)
,不是吗?它是否等待取决于您将callback()
放在何处。如果将其放入slideUp
回调中,它将等待动画完成。如果你把它放在外面,它会在你启动动画后立即运行。当你调用一个执行异步动作的JS函数时,比如动画或AJAX调用,函数会在启动动作后立即返回,它不会等待动作完成。