2013-02-17 5 views
0

jQuery 용 플러그인을 만들고 있는데 애니메이션이 끝난 후에 같은 함수를 다시 실행해야합니다. 애니메이션 기능을 setTimeout() 사용하여이 작업을 위해 노력하고있어.애니메이션 내에서 jQuery 부모 함수 실행

콘솔 오류 메시지가 catch되지 않은 형식 오류

jQuery(theslide).animate(theanimation,1000,function() { 
     setTimeout('this.makemeslide()',3000) 
}) 

입니다 내가 함께 사투를 벌인거야 코드는 하단에

입니다 : 개체 [개체 창] 때문에 본하게있는 방법 'makemeslide'이 없습니다 이것은 움직이는 물체의 지점입니다. 그러나 나는 나의 목표를 달성하는 방법을 모른다. 코드 문자열 여기

내 전체 코드

jQuery.fn.daSlider = function(options) { 
    //Options 
    var settings = $.extend({ 
      'selector'   : 'slider', 
      'width'  : 940, 
      'height'  : 380 
     }, options); 

    //globals 
    var sliderarray = []; 
    var slidercount = 0; 


    //Contruct 
    this.construct = function() 
    { 
     jQuery('.slider .slide').each(function(){ 

       jQuery(this).css('display','none'); 
       sliderarray.push(this) ; 

       jQuery(this).children().each(function(){ 
        jQuery(this).css('display','none'); 
        sliderarray.push(this) ; 
       }); 
     }); 

     this.makemeslide(); 
    } 
    //run the constuct 

    this.makemeslide = function() 
    { 
     theslide = sliderarray[slidercount]; 
     slidercount++; 

     way_to_slide = jQuery(theslide).attr('slide'); 

     slide_position = jQuery(theslide).position(); 
     //console.log(slide_width); 
     //console.log(slide_height); 


     if(way_to_slide == 'right') 
     { 
     jQuery(theslide).css('left', slide_position.left + settings.width); 
     theanimation = {'left' : '-='+settings.width}; 
     } 
     if(way_to_slide == 'left') 
     { 
     jQuery(theslide).css('left', slide_position.left - settings.width); 
     theanimation = {'left' : '+='+settings.width}; 
     } 
     if(way_to_slide == 'up') 
     { 
     jQuery(theslide).css('top', slide_position.top - settings.height); 
     theanimation = {'top' : '+='+settings.height}; 
     } 
     if(way_to_slide == 'down') 
     { 
     jQuery(theslide).css('top', slide_position.top + settings.height); 
     theanimation = {'top' : '-='+settings.height}; 
     } 
     if(way_to_slide == 'fade') 
     { 
     jQuery(theslide).css('opacity', 0); 
     theanimation = {'opacity' : 1}; 
     } 

     jQuery(theslide).css('display','block') ; 
     jQuery(theslide).animate(theanimation,1000,function(){setTimeout('this.makemeslide()',3000)}) 

    } 
    this.construct(); 

}; 
+0

'this'이 될 두 번째 경고가 당신에게 윈도우 객체가 아닌 오 - 객체를 보여줄 것

function Object(){ self=this; this.inner = function(){ setTimeout(function(){ alert(self); alert(this); },100); } } var o = new Object(); o.inner(); 

주 ? 이 'makemeslide' 및'construct' 속성을 할당하는 것은 jQuery 래퍼 객체입니다. – Bergi

+0

도움을 주신 모든 분들께 감사드립니다. –

답변

1

절대 사용하지 마십시오 setTimeouteval 에드 될 수 있습니다! 또한 this reference은 상위 기능의 것이 아닙니다.

var plugin = this; 
jQuery(theslide).animate(theanimation, 1000, function() { 
    // "this" references theslide element 
    setTimeout(function() { 
     // "this" references the global object (window) 
     plugin.makemeslide(); 
    }, 3000); 
}); 

, BTW 대신 독립적 인 타임 아웃의 당신은 콜백 jQuery's delay method을 사용할 수 있습니다.

+0

정말 대단합니다. 그것은 효과가 있었다. 당신들은 너무 좋아요. D –

+0

지연 기능을 살펴 보았습니다. –

0

이것은 의 자바 스크립트는 자바와 다릅니다. 이 경우이이 경우의 창 개체입니다.

이 문제를 방지하기 위해 클로저를 사용할 수

: 당신이 예상하는 일

관련 문제