Bootstrap toast - Data delay not respected - Toast dont hide

人盡茶涼 提交于 2021-01-29 09:56:19

问题


After hit the submit button of a form (Ajax POST) I want to refresh the toast with the message generated in Django view. I succeed to show the message but the data-delay is not respected, the toast does not disappear after 5 sec.

<div id="messages" aria-live="polite" aria-atomic="true" style="position: relative">
        <div style="position: absolute; top: 12px; right: 12px;" >
            {% for message in messages %}
                    <div class="toast d-flex toast-success" role="alert" aria-live="assertive" aria-atomic="true" data-delay="3000">
                        <div class="toast-header toast-success shadow-none" >
                            <i class="fas fa-check"></i>
                        </div>
                        <div class="toast-body">
                            {{message}} 
                        </div>
                    </div>
            {% endfor %}
        </div>
    </div>

And in success part of Ajax, I have the following code:

$('#messages).load(location.href + " #messages>*", "") 

回答1:


You can use setTimeout in this case:

$("#messages").load(location.href + " #messages>*", "");
setTimeout(function(){$("#messages").empty();}, 5000);

Update

Prevent the function set with the setTimeout() to execute:

var myTimeout;

function myFunction() {
  clearTimeout(myTimeout);
  $("#messages").load(location.href + " #messages>*", "");
  myTimeout = setTimeout(function(){$("#messages").empty();}, 5000);
}


来源:https://stackoverflow.com/questions/65781565/bootstrap-toast-data-delay-not-respected-toast-dont-hide

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