2012-03-01 2 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'は起動しますか? 'data'のデータ型は' 'condition''の型と一致しますか? –

+0

動作していないのはどうですか?残りのコードはどこですか? – j08691

+0

問題は何か分かりませんが、同じ関数のelseブランチでクリアする場合にタイムアウトを設定するのはなぜですか?なぜ 'setTimeout()'をifブランチに移動させないのですか? – nnnnnn

答えて

1

これは(jQueryのを使用して、それは結論を変更していない)罰金、テストコードを動作しているようだ:

HTML

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

js

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'); 

のdivに出力し、この:

clearing hi other 

予想通り。ライブ例:

・ホープ、このことができます。

+0

編集した投稿をご覧ください。私はちょうど全体のコードを追加しました。 – Jay

+0

コードに 'other_function'はありません。あなたの問題を示すjsfiddleを作ることができます。それ以外の場合は、問題がどこにあるのか分かりづらいことがたくさんあります。 –

+0

リンクはこちらhttp://jsfiddle.net/jonevar/2Z2NQ/5/ – Jay

関連する問題