jQuery 如何在会话超时时关闭所有活动的引导模式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17978043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:18:10  来源:igfitidea点击:

How to close all active bootstrap modals on session timeout?

javascriptjquerytwitter-bootstrapmodal-dialog

提问by jeff werner

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.

我需要在用户空闲时拨打电话并传递将关闭所有 Bootstrap 模式的会话超时。处于活动状态的模态取决于用户当时在做什么,所以我想做一些包罗万象的事情。

I tried:

我试过:

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

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

当超时发生但我的模态仍然存在时。

回答by S. Rasel

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
});

回答by Tom McDonough

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.

如果您希望用户像往常一样使用页面,第二行至关重要。

回答by hakuna

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.$emit('logout');,我的服务中的侦听器如下:

$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();

如果您想隐藏任何其他模态,例如角度材质对话框 ( $mdDialog) 和甜蜜警报对话框的使用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.

我不知道这是否是正确的方法,但它对我有用。

回答by neTurmeric

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

试试这个方法: $('.modal.in:visible').modal('hide');