2012-03-01 3 views
-4

이것이 작동하지 않는 이유를 아는 사람이 있습니까? 여기 상태가있는 ClearTimeout이 작동하지 않습니다.

http://jsfiddle.net/jonevar/2Z2NQ/5/

전체 코드입니다 : 이것은 좋은, 테스트 코드 작동하는 것 같군

function ag_alert(message) { 

    event.preventDefault(); 

    //SetTimeout in case didn't close manualy 
    var timer = setTimeout(cls_message, 5000), 
     cur_url = window.location.href; 

    //Check if its already on 
    if (! $('.ag_alert_wrapper').is(':visible')) { 

     //Set the language 
     (cur_url.indexOf('/en/') >= 0) ? cls_txt = "close" : cls_txt = "閉じる" ; 

     $('<div class="ag_mess ag_alert_wrapper"></div><div class="ag_mess ag_alert_wrapper_close">'+ cls_txt +'</div>') 
      .prependTo('body'); 
     $('.ag_alert_wrapper') 
      .append('<p>'+ message +'</p>') 
      .animate({top : 0}, 200, function() { 
       $('.ag_alert_wrapper_close') 
       .animate({top : 90}, 200) 
       .on({ 
        mouseenter : function() { 
         $(this).animate({ 
          top : 100 
         }, 200); 
        }, 
        mouseleave : function() { 
         $(this).animate({ 
          top : 90 
         }, 200); 
        }, 
        click : function() { 
         cls_message(); 
        } 
       }); 
      }); 

     //Setups ESC key to close message 
     $(document).keydown(function(e) { 
      if (e.keyCode === 27) { 
       cls_message(); 
      } 
     }); 

    } else { 
     //if Alert is already visible 
     $('.ag_alert_wrapper') 
      .children('p').html(message) 
      .end() 
      .effect("highlight", { 
       color : '#FF0' 
      }, 1000); 

     clearTimeout(timer); 
    } 

} 


function cls_message() { 
    $('.ag_mess').animate({ 
     top : -200 
    }, 200, function() { 
     $('.ag_mess').remove(); 
    }); 
} 
+0

무슨 일입니까? 'other_function'이 실행 중입니까? '데이터'의 데이터 유형이 '조건'의 유형과 일치합니까? –

+0

작동하지 않는 것은 어떨까요? 나머지 코드는 어디에 있습니까? – j08691

+0

문제가 무엇인지 모르겠지만 동일한 함수의 else 분기에서이를 지우려면 시간 초과를 설정해야합니다. 왜'setTimeout()'을 if 브랜치로 옮기지 않겠습니까? – nnnnnn

답변

1

(jQuery를 사용하여,하지만 결론 변경하지 않는 것) :

HTML

<div id="msgs"></div> 

js

사업부에 5,
function other_function() { 
    $('#msgs').append('other '); 
} 

function do_something(data) { 
    var timer = setTimeout(other_function, 500); 
    if (data === "condition") { 
     $('#msgs').append('hi '); 
    } else { 
     $('#msgs').append('clearing '); 
     clearTimeout(timer); 
    } 
} 

do_something('yay'); 

do_something('condition'); 

출력이이 :

clearing hi other 

예상대로. 라이브 예 :이 도움이

희망.

+0

편집 된 게시물을 살펴 보시겠습니까? 방금 전체 코드를 추가했습니다. – Jay

+0

코드에는 'other_function'이 없습니다. 문제를 나타내는 jsfiddle을 만들 수 있습니까? 그렇지 않으면 추가 한 코드에 문제가있는 곳을 파악하기 어려울 정도로 많은 것들이 있습니다. –

+0

여기 링크 http://jsfiddle.net/jonevar/2Z2NQ/5/ – Jay

관련 문제