2013-10-13 2 views
0

기본적으로 화면이 있으며 단추를 연결하면 화면이 위로 이동 한 다음 다른 화면이 나타납니다. 문제는 slideUp() 함수가 실행하는 데 시간이 걸리고 slideUp 함수가 실행 완료되기 전에 다른 화면이 표시된다는 것입니다. 다른 화면이 나타날 때까지 slideUp 기능이 실행될 때까지 다른 화면이 대기 할 수있게하려고합니다. 다음은 주요 자바 스크립트는 다음과 같습니다

$('#sidebar ul li a').click(function(){ 

    $('#sidebar ul .clicked').removeClass('clicked'); // when an <a> is clicked, remove .clicked class from any other <a>'s 

    $(this).addClass('clicked'); 

    hidePrevSlide(); //this calls slideUp in the current screen 
    $(this).showCurrentSlide(); //this shows another screen before hidePrevSlide is even finished executing/sliding Up 
}); 

내가
hidePrevSlide(function(){ 
    $(this).showCurrentSlide(); 
}); 

을 시도했지만 다음이 어떤 이유에 대한 코드를 나누기. 내가하는 일을 성취 할 수있는 방법이 있습니까?

+0

jsfiddle 수 있습니까? – stevemarvell

+0

물론 시도해 보겠습니다. 5 분을주세요. – user2817200

+0

@stevenmarvell 편집 : 좋아요, 함수 안에있는 함수에서 $ (this)를 사용하는 방법이 있다면 알려주시겠습니까? 또는 $ (this) .showCUrrentSlide()를 실행하기 전에 3 초 기다리는 방법; 선? 나는 setTimeout을 사용하려했지만 작동하지 않았다. jsfiddle'ing은 장고를 사용하고 있기 때문에 꽤 많은 시간이 걸릴 것이고 html은 많은 파이썬 코드와 변수와 섞여있다. – user2817200

답변

1

이 시도 :

$('#sidebar ul li a').click(function(){ 

    $('#sidebar ul .clicked').removeClass('clicked'); // when an <a> is clicked, remove .clicked class from any other <a>'s 

    $(this).addClass('clicked'); 

    var current_slide=$(this); 
    hidePrevSlide(function(){ 
     current_slide.showCurrentSlide(); 
    }); 
    $(this).showCurrentSlide(); //this shows another screen before hidePrevSlide is even finished executing/sliding Up 
}); 

난 당신이 범위 지정 문제로 실행하는 것을 볼 수 있습니다. 함수 앞에 $(this)을 저장하면 아래 함수에서 해당 변수에 액세스 할 수 있습니다.

작은 최적화를위한

, 당신은 우리가 지금처럼 우리 current_slide 변수를 다시 사용할 수있는 여러 $(this)을 사용하지 않아야합니다 : 당신이 그것을

$('#sidebar ul li a').click(function(){ 
    var current_slide=$(this); 

    $('#sidebar ul .clicked').removeClass('clicked'); // when an <a> is clicked, remove .clicked class from any other <a>'s 

    current_slide.addClass('clicked'); 

    hidePrevSlide(function(){ 
     current_slide.showCurrentSlide(); 
    }); 
    current_slide.showCurrentSlide(); //this shows another screen before hidePrevSlide is even finished executing/sliding Up 
}); 
+0

마지막'showCurrentSlide()'콜을 제거해야합니다 – stevemarvell

+0

흠, 당신의 코드를 사용했지만 여전히 똑같은 것을 제공합니다 문제. 나는 current_slide를 사용하여 setTimeout을 사용하려고 시도 할 것이고 그것이 작동하는지 볼 것이다. – user2817200

+0

퍼펙트, 알았어. 솔루션은 사실 $ (this)를 함수 앞에 변수에 저장하는 것이었다. 나는 이유를 모르겠다 hidePrevSlide (function() { current_slide.showCurrentSlide(); }}); 작동하지 않았다 그러나 나는 변수에 저장 current_slide를 한 다음 한 \t window.setTimeout (함수() { \t current_slide.showCurrentSlide(); \t}, 500); 그리고 그것이 작동 ... 감사합니다! 또한, stackoverflow에 대한 의견을 말할 때 주석 안에 코드를 작성하는 방법은 무엇입니까? – user2817200

관련 문제