How to close all active bootstrap modals on session timeout?

冷暖自知 提交于 2019-12-03 14:25:24

问题


I need to make a call when the user is idle and passes the session time out that will close all Bootstrap modals. The modals being active are dependent on what the user is doing at the time so I would like to do something that's is all encompassing.

I tried:

$('.modal').modal('toggle');

When the time out occurs but my modals are still there.


回答1:


Use the following code:

$('.modal').modal('hide');

Also if you would like to do something if the modal is hidden then you can do this:

$('.modal').on('hidden', function () {
  // write your code
});



回答2:


The correct answer is missing something vital.

$('.modal').modal('hide') // closes all active pop ups.
$('.modal-backdrop').remove() // removes the grey overlay.

The second line is vital if you want the users to use the page as normal.




回答3:


Try this way : $('.modal.in:visible').modal('hide');




回答4:


This is how i got it working in my project without using any factory or additional code.

//hide any open bootstrap modals
  angular.element('.inmodal').hide();

I have a timeout function that emits logout as $rootScope.$emit('logout'); and the listener in my service is as follows:

$rootScope.$on('logout', function () {                    
                    //hide any open bootstrap modals
                    angular.element('.inmodal').hide();

                    //do something else here  

                });

If you want to hide any other modals such as angular material dialog ($mdDialog) & sweet alert dialog's use angular.element('.modal-dialog').hide(); & angular.element('.sweet-alert').hide();

I don't know if this is the right approach , but it works for me.



来源:https://stackoverflow.com/questions/17978043/how-to-close-all-active-bootstrap-modals-on-session-timeout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!