How to open a jQuery Mobile popup programmatically and close it after 5 seconds

Make sure your code is within a page event handler like pagecreate so that jQM has initialized the popup widget:

<div data-role="page" id="page1">
     <div data-role="header">
        <h1>My page</h1> 
    </div>  
    <div role="main" class="ui-content">
        <button id="btnpopup">Show Dynamic Popup</button>
    </div> 

    <div data-role="popup" id="p" data-position-to="window" data-transition="turn"><p>This is a completely basic popup, no options set.</p></div>
</div>  

$(document).on("pagecreate","#page1", function(){ 
    $("#btnpopup").on("click", function(){
        $("#p").popup("open"); 
        setTimeout(function(){  $("#p").popup("close"); }, 5000);
    });
});

Working DEMO


it is actually simple:

 $("#chatWindow").popup("open");