最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to auto close an alert after few seconds with Bootstrap? - Stack Overflow

programmeradmin1浏览0评论

I have developed a Python/Flask application and ran into an issue implementing a Bootstrap alert where the idea is to:

  • dismissible via a close button.
  • fade out and close after 2s if no action is taken.

Code

<div class="container">
        {% for message in get_flashed_messages() %}   
        <script>       
          function close_alert()
          {
            document.getElementById("my_alert").close()
          }
          setTimeout("close_alert()", 2000)
        </script>
        <div id="my_alert" class="alert alert-primary alert-dismissible fade show" role="alert">
          <strong>{{message}}</strong>
          <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        {% endfor %}
</div>

I'm fairly new to Bootstrap and I would appreciate any support!

Thanks!

I have developed a Python/Flask application and ran into an issue implementing a Bootstrap alert where the idea is to:

  • dismissible via a close button.
  • fade out and close after 2s if no action is taken.

Code

<div class="container">
        {% for message in get_flashed_messages() %}   
        <script>       
          function close_alert()
          {
            document.getElementById("my_alert").close()
          }
          setTimeout("close_alert()", 2000)
        </script>
        <div id="my_alert" class="alert alert-primary alert-dismissible fade show" role="alert">
          <strong>{{message}}</strong>
          <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        {% endfor %}
</div>

I'm fairly new to Bootstrap and I would appreciate any support!

Thanks!

Share Improve this question edited Jun 28, 2021 at 18:59 Gass 9,4605 gold badges47 silver badges53 bronze badges asked Jun 28, 2021 at 14:37 ftaniftani 1782 gold badges4 silver badges14 bronze badges 1
  • You can use .hide() rather than .close() in close_alert function. – charchit Commented Jun 28, 2021 at 14:40
Add a ment  | 

2 Answers 2

Reset to default 1

You can use the Bootstrap methods to achieve this. Take a look here at the bottom of the page for more info.

Logic

When the button is clicked jQuery removes the class d-none of the alert element which makes it visible. And then by using setTimeout() JS will wait 2000ms before closing the alert with alert() bootstrap 4 method.

It will work for only one click.

Note: using Bootstrap 4 methods requires to work with jQuery.

$('#btn').click(function(){

  $('#alert').removeClass('d-none');
  
  setTimeout(() => {
    $('.alert').alert('close');
  }, 2000);
  
})
<script src="https://cdn.jsdelivr/npm/[email protected]/dist/jquery.slim.min.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr/npm/[email protected]/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn./bootstrap/4.0.0/css/bootstrap.min.css" crossorigin="anonymous">
 
<div id="alert" class="alert alert-primary m-3 d-none" role="alert">
  This is a primary alert—check it out!
</div>

<button class="btn btn-primary m-3" id="btn">
  show alert and close it in 2 seconds
</button>

With plain JavaScript

This code will show the alert every time the user clicks the button. In case you don't want that, you could use a Boolean or a session storage variable bined with a conditional statement to make it work only once.

const btn = document.getElementById('btn'),
      alert = document.getElementById('alert');
      
btn.addEventListener('click', () => {
  
  alert.classList.remove('d-none');
  
  setTimeout(() => {
    alert.classList.add('d-none');
  }, 2000)
  
})
<script src="https://cdn.jsdelivr/npm/[email protected]/dist/jquery.slim.min.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr/npm/[email protected]/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn./bootstrap/4.0.0/css/bootstrap.min.css" crossorigin="anonymous">
 
<div id="alert" class="alert alert-primary m-3 d-none" role="alert">
  This is a primary alert—check it out!
</div>

<button id="btn" class="btn btn-primary m-3">
  show alert and close it in 2 seconds
</button>

I would suggest using a "toast"-- this is a UI ponent intended for your use case: https://getbootstrap./docs/4.2/ponents/toasts/

From Bootstrap docs:

<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
  <div class="toast-header">
    <img src="..." class="rounded mr-2" alt="...">
    <strong class="mr-auto">Bootstrap</strong>
    <small class="text-muted">11 mins ago</small>
    <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
  <div class="toast-body">
    Hello, world! This is a toast message.
  </div>
</div>

发布评论

评论列表(0)

  1. 暂无评论