2017-02-13 22 views
0

現在、jQueryを使用してドロップダウンメニューを操作しています。タイムアウト機能がまったく動作しない問題が発生しました。そのためのコードは次のとおりです。jQueryタイムアウト関数が動作しない

$(document).ready(function() { 
 
    $('.has-sub').hover(
 
    function() { 
 
     $('ul', this).stop(true, true).slideDown(500); 
 
    }, 
 
    function() { 
 
     $('ul', this).stop(true, true).slideUp(400); 
 
    }, 
 
    function() { 
 
     setTimeout(function() { 
 
     $('.has-sub').addClass("tap"); 
 
     }, 2000); 
 
    }, 
 
    function() { 
 
     $(this).removeClass("tap"); 
 
     clearTimeout(); 
 
    } 
 
); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>

は、私は何をしようとするとドロップダウンの親のためのホバー遅延を作成することです。ドロップダウンメニューが表示されるように、親を2秒間ホバーする必要があります。私はまた、それをSlidedownとSlideupエフェクトとペアにしたい。

SlidedownとSlideupは正しく機能しますが、Timeoutは機能しません。

+5

もう一度、[ドキュメント](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/clearTimeout)の読み取りは常に便利です。.. 。 – Teemu

+0

これは、jQueryを使用していることが間違っていることを意味し、setTimeoutはJSに属します。 – ymz

+3

jQueryの '.hover()'メソッドは、1つまたは2つの関数をパラメータとして受け取ります。 4. https://api.jquery.com/hover/ – blex

答えて

1

clearTimeout()(これはJQueryの一部ではありません)を呼び出すことはできません。キャンセルするタイマーの識別子を入力する必要があります。

また、setTimeout()clearTimeout()は、その点でJQueryまたはJavaScriptの一部ではありません。これらは、ブラウザによって提供されるwindowオブジェクトのメソッドです。それらは言語(JavaScript)またはライブラリ(JQuery)の一部ではありません。

また、JQuery .hover() methodは、2つの引数を取り、あなたは、私は以下それらを組み合わせた、しかし、あなたがやろうとしている正確に何を知っていない4.を提供している、あなたはそれを調整する必要があります。

$(document).ready(function() { 
 
    
 
    // This will represent the unique ID of the timer 
 
    // It must be declared in a scope that is accessible 
 
    // to any code that will use it 
 
    
 
    var timerID = null; 
 
    
 
    $('.has-sub').hover(
 
    function() { 
 
     
 
     // Clear any previously running timers, so 
 
     // we dont' wind up with multiples. If there aren't 
 
     // any, this code will do noting. 
 
     clearTimeout(timerID); 
 
     
 
     $('ul', this).stop(true, true).slideDown(500); 
 
     // Set the ID variable to the integer ID returned 
 
     // by setTimeout() 
 
     timerID = setTimeout(function() { 
 
     $('.has-sub').addClass("tap"); 
 
     }, 2000); 
 
    }, 
 
    function() { 
 
     $('ul', this).stop(true, true).slideUp(400); 
 
     $(this).removeClass("tap"); 
 
     // Clear the particular timer based on its ID 
 
     clearTimeout(timerID); 
 
    } 
 
); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>

+2

@blexのコメントを参照 – gaetanoM

+1

@gaetanoMそれに対処するために更新されました。 –

+0

"あなたがやろうとしていることを正確に知らない"これは問題です.. – gaetanoM

関連する問題