简体   繁体   中英

How to create a dialog box that appears without any event triggered in jQuery?

I'm using jQuery 1.9.1, and jQuery Mobile 1.3.1, and I want to create a dialog box, with only one button, informing the user that their time is up in my game. The point is that, as long as I can see dialogs in jQuery appear after certain eventType. For example, after a button is clicked the dialog can appear. But in my situation I don't have any event, it must just appear automatically when the condition in the if statement is true. And when the user clicks OK button on the widget, he/she should be transferred to the page referred below. Any idea how to achieve it?

function showTimer() {
    if (timestamp.getMinutes().pad(2) == "59" && timestamp.getSeconds().pad(2) == "59") {
        // dialog goes here...
        $.mobile.navigate("#finalPage");
    }
    return false;
}

使用setInterval启动计时器,并每隔x毫秒检查一次条件

It's all possible using jQuery:

//Initialise the dialog first
var dialog = $('<p>Are you sure?</p>').dialog({
    autoOpen:false,
    buttons: {
        "Cancel": function () {
            $.mobile.navigate("#finalPage"); // Your custom code for navigation on button click
        }
    }
});
setTimeout(function(){ // When your condition is met, just call the dialog using this code
    dialog.dialog('open');
},2000);

Here,I have used timeout just to show how you can open the dialog at any stage without triggering any click.

Demo : http://jsfiddle.net/lotusgodkk/GCu2D/175/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM