How do I time a modal pop up?

2019-08-27 18:56发布

问题:

I have a function that does some database update in asp.net. I'd like a modal popup to show a "success" message for just 5 seconds after my function has been called. In this case, the modal popup would not be triggered by any "TargetControl" but would show up for just 5 seconds once the function is done.

Thanks

回答1:

You can't close standard javascript modal dialogs (alert, confirm,..) after a timeout. Only manual close works with them.

But, you can use jquery/UI dialog:

// timeOut in ms
function showMessageWithTiemout(message, timeOut){

    // show dialog
    var successDialog = $('<div>'+message+'</div>').dialog({modal: true});  

    //close it after 5 seconds
    setTimeout(function(){ successDialog.dialog('close'); }, timeOut);

}

//usage:
showMessageWithTiemout('success!', 5000);


回答2:

You have to manually call the show method on the panel like:

var pnl = $find("<%= modal.ClientID");
pnl.show();

So you can use window.setTimeout to call this:

window.setTimeout(function() { /* code */ }, 5000);

But it can't just happen very easily.

HTH.