Search code examples
jqueryjquery-mobilesetintervaljquery-mobile-popup

How to have the popup dialog disappear after some time?


How can I with jQuery + jQuery Mobile. Have an popup dialog to disappear after some time. This one I written work to some extent. It will disappear after sometime. However when I click the button to activate it again it doesn't work, unless I refresh the page again.

JavaScript

<script type="text/javascript">
     $(document).on('pageinit', function(e) {
        $('#postnote').click(function() {
            $('#dialog').popup('open', {history: false}).delay(500).fadeOut('slow').hide();
        });
    });
</script>

The page

<div data-role="page" id="addnote">
    <div id="dialog" data-role="popup" data-transition="fade">
        <div data-role="header"><h1>Note posted</h1></div>
    </div>
    <div data-role="header" data-theme="a">
        <h1>Add Note</h1>
    </div>
    <div data-role="content" data-theme="b">
        <textarea id="note" rows="40" name="note"></textarea>
        <a href="#" id="postnote" data-role="button" data-transition="fade" data-theme="b">Post</a>
        <a href="#" data-rel="back" data-role="button" data-transition="slidefade" data-theme="a" data-direction="reverse">Back</a>
    </div><!-- /content -->
</div><!-- /page -->

Solution

  • You can do it this way.

    $(document).on('popupafteropen', '.ui-popup', function() {
     setTimeout(function () {
      $(this).popup('close');
     }, 5000);
    });