Ivor Kovic
Ivor Kovic

Reputation: 53

JQM: popup not closing on timeout?

I have managed to get a popup to open on page load, but only the first time that page is opened. Once it opens, I want it to close on its own after several seconds, but I am not able to do that.

This is the code I am using:

<script type="text/javascript" language="JavaScript">
    $(document).on('pageshow', function (event) {
if (sessionStorage.popUpShown != 'true') {

    $('#strelica').popup('open', {positionTo: '#hitnizbor'});

    sessionStorage.popUpShown = 'true';
    setTimeout(function () {
        $("#strelica").popup("close");
    }, 3000);
}

else{
    alert('already shown the popup');
}

});
</script>

Upvotes: 2

Views: 546

Answers (2)

sayume
sayume

Reputation: 128

You are using jquery plugin? I found the answer in jquery plugin popupjs:

$('a.popup').popup({
   afterOpen : function(){
     var popup = this;
     setTimeout(function(){
        popup.close();
    }, 2000);
}
});

It is possible you miss the official sample.

Upvotes: 0

Gajotres
Gajotres

Reputation: 57309

Your example should work, but I made you a safer version: http://jsfiddle.net/Gajotres/Uauar/

$(document).on('pageshow', '#index', function(){     
    var start = setTimeout(function () {
        $('#strelica').popup('open', {positionTo: '#hitnizbor'});
        clearInterval(start);
    }, 0);    

    var stop = setTimeout(function () {
        $("#strelica").popup("close");
        clearInterval(stop);        
    }, 3000);
});

This code can be used in every page event, not only pageshow. If you want it to be executed only once use pageinit.

ClearInterval is here to prevent constant opening and closing. If you have more questions feel free to ask.

Upvotes: 3

Related Questions