简体   繁体   English

jQuery如何使弹出窗口在计时器上出现/消失?

[英]jQuery How to make a popup Appears / Disappears on timer?

When my website loads, the popup appears - I need to make it automatically close after a specific time. 网站加载后,将显示弹出窗口-我需要在特定时间后使其自动关闭。

$(document).ready(function () {

    //select the POPUP FRAME and show it

    $("#popup").hide().fadeIn(1000);

    //close the POPUP if the button with id="close" is clicked
    $("#close").on("click", function (e) {
        e.preventDefault();
        $("#popup").fadeOut(1000);
    });

}); 

There is already a button but i need to remove it. 已经有一个按钮,但是我需要将其删除。

You can use the delay() function for that: 您可以为此使用delay()函数:

 $(document).ready(function() { $("#popup").hide().fadeIn(1000).delay(5000).fadeOut(1000); $("#close").on("click", function(e) { e.preventDefault(); $("#popup").fadeOut(1000); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="popup">popup</div> 

Please mind the note that is given on the documentation: 请注意文档中的注释:

The .delay() method is best for delaying between queued jQuery effects. .delay()方法最适合在排队的jQuery效果之间进行延迟。 Because it is limited—it doesn't, for example, offer a way to cancel the delay—.delay() is not a replacement for JavaScript's native setTimeout function, which may be more appropriate for certain use cases. 因为它是有限的(例如,它没有提供消除延迟的方法),所以.delay()不能替代JavaScript的本机setTimeout函数,后者可能更适合某些用例。

Javascript have option setTimeout .setTimeout is a native JavaScript function (although it can be used with a library such as jQuery, as we'll see later on), which calls a function or executes a code snippet after a specified delay (in milliseconds). Javascript具有选项setTimeout .setTimeout是一个本机JavaScript函数(尽管它可以与jQuery之类的库一起使用,我们将在后面介绍),该函数在指定的延迟(毫秒)后调用一个函数或执行一个代码片段。 。

setTimeout(function() {
      $("#popup").fadeOut(1000);
 }, 1000);

or in jquery use .delay() . 或在jquery中使用.delay() Set a timer to delay execution of subsequent items in the queue. 设置计时器以延迟队列中后续项目的执行。

$("#popup").delay(1000).fadeOut(1000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM