auto close popup after second

I am using simple popup and I want to auto close popup after seconds. Please guide me how to do that

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

Upvotes: 0

Views: 1493

Answers (2)

Kulvar
Kulvar

Reputation: 1179

The callback given to setTimeout has no "this" value. Use a variable to store $(this) and use it in your setTimeout callback

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

Upvotes: 2

Kresimir Pendic
Kresimir Pendic

Reputation: 3614

Try this,, you are loosing the this scope inside setTimeout callback, see if that works

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

EDIT:

I've updated your fiddle, see it here: https://jsfiddle.net/8vx0185d/1/

Upvotes: 2

Related Questions