秒后自动关闭弹出窗口

时间:2017-11-23 14:59:29

标签: javascript jquery

我正在使用简单的弹出窗口,我希望在几秒钟后自动关闭弹出窗口。请指导我如何做到

jQuery(document).ready(function($) {
    $(".movenext").on("click", function(e) {
        e.preventDefault();
        $(this).simplePopup({
            type: "html",
            htmlSelector: "#popupuser"},
            setTimeout(function(){
                $(this).simplePopup().close();
            }, 3000)
        );                                      
    });
});

2 个答案:

答案 0 :(得分:2)

给setTimeout的回调没有"这个"值。使用变量存储$(this)并在setTimeout回调中使用它

jQuery(document).ready(function($) {
    $(".movenext").on("click", function(e) {
        e.preventDefault();
        var element = $(this);
        element.simplePopup({
            type: "html",
            htmlSelector: "#popupuser"
        });
        setTimeout(function(){
            element.simplePopup().close();
        }, 3000);
    });
});

答案 1 :(得分:2)

试试这个,你在this回调中失去了setTimeout范围,看看是否有效

jQuery(document).ready(function($) {
    $(".movenext").on("click", function(e) {
        e.preventDefault();
        var self = this;
        $(this).simplePopup({
                type: "html",
                htmlSelector: "#popupuser"
        });
        setTimeout(function() {
            $('.simple-popup-content .close').click();
        }, 3000);
    });
});

编辑:

我已经更新了你的小提琴,请在此处查看:https://jsfiddle.net/8vx0185d/1/