如何在某段延遲後呼叫 jQuery 函式?
若要在某段延遲後呼叫一個 jQuery 函式,請使用 siteTimeout() 方法。在此,jQuery fadeOut() 函式在幾秒後呼叫。
你可以嘗試執行以下程式碼來了解如何在 jQuery 中使用 setTimeout() 方法以在延遲後呼叫一個 jQuery 函式 −
示例
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("#button1").bind("click",function() { setTimeout(function() { $('#list').fadeOut();}, 2000); }); }); </script> </head> <body> <input type="button" id="button1" value="Fade Out" /> <br/> <br/> <div id="list"> <ul> <li>Voice</li> <li>Speak</li> <li>Write</li> </ul> </div> <p>The above content will fade out after 2 seconds</p> </body> </html>
廣告