简体   繁体   English

一段时间后关闭Bootstrap模式

[英]Close Bootstrap Modal After a Period of Time

I have a gif image which spins in circle. 我有一个旋转的gif图像。 I put that in the bootstrap modal. 我把它放在引导模态中。 It is my progress bar. 这是我的进度栏。 I would like the modal to appear for about 5 seconds and then it will close itself. 我希望该模式出现约5秒钟,然后它会自行关闭。 Here's my code: 这是我的代码:

<?php
function progressBar($total){
   sleep($total);

   return "modal";
}
?>

<!-- Modal ProgressBar -->
<div class="modal fade" id="progressBarCenter" tabindex="-1" role="dialog" aria-labelledby="progressBarCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="progressBarLongTitle">Your Script Is Executing...</h5>
        <button type="button" class="close" data-dismiss=<?php progressBar(5); ?> aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <img src="webapps/images/progressBar.gif">
      </div>
    </div>
  </div>
</div>

It sort of work. 有点工作。 As in, it sleeps for 5 seconds then it loads the whole page. 在这种情况下,它会休眠5秒钟,然后加载整个页面。 Not what I want at all. 根本不是我想要的。 A user would click on a button to startup the modal, no problem there. 用户可以单击一个按钮来启动模态,那里没有问题。 Once it is active, it need to sleep for 5 sec, and then it close itself. 一旦激活,它需要睡眠5秒钟,然后关闭自身。 As in, it closes the current or active modal. 与之类似,它关闭当前或活动模式。

How would I go about doing this? 我将如何去做呢?

You can use the following script to close a Bootstrap Modal after 5000 ms: 您可以使用以下脚本在5000毫秒后关闭Bootstrap Modal:

<script>
    $(document).ready(function () {
        $("#buttonID").click(function(){
            setTimeout(function() {
                $('#progressBarCenter').modal('toggle');
            }, 5000);
        });
    });
</script>

PHP as you have coded will not work. 您编码的PHP无法正常工作。

Try use Javascript click() fuction for a button and the sleep() function you have used: 尝试对按钮和使用过的sleep()函数使用JavaScript click()功能:

$(document).ready(function(){
        $('button#ajaxSubmit').click(function(e){
            setTimeout($total);
            $('#progressBarCenter').modal('toggle');
       });
});

The first line of this JS will wait for the entire page to load before let the code to run. 该JS的第一行将等待整个页面加载,然后再运行代码。 The second line will run sleep() after the user click the form button, and after that, toggle the modal. 用户单击表单按钮后,第二行将运行sleep(),然后切换模式。

And add a id to your <button> , like the one below: 并向您的<button>添加一个id ,如下所示:

<button type="button" class="close" aria-label="Close" id="ajaxSubmit">

I did a debugger in IE and realize that you can't use a sleep function in javascript cause it doesn't exist. 我在IE中做了一个调试器,意识到您不能在javascript中使用sleep函数,因为它不存在。 You need to use the setTimeout() function instead. 您需要改用setTimeout()函数。 Thank you @Chukwuemeka Inya 谢谢@Chukwuemeka Inya

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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