I am trying to automatically close Bootstrap modality after a set period of time.
Here is the js code that I use to close the modality in 4 seconds:
setTimeout(function() { $('#myModal').modal('hide'); }, 4000);
Two main problems:
(A) When an html page (containing modal files) is loaded, the modal timeout seems to be executed before it is modally displayed. After clicking on the link on the page, the modal mod will be displayed. If the link is not clicked immediately when the page loads, the modal will be displayed only briefly , and then immediately close, because essentially the waiting period begins when the html page is loaded, and not when the modal form is displayed.
(B) If the user clicks on the link to launch the modal a second time (or 3rd, 4th time, etc.), the modal is displayed correctly, but DOES NOT close after a period of time. It just remains open until the user closes it manually.
So, two questions:
(1) How can I get a modal waiting period until the modal text appears before the clock starts.
(2) How do I get a modal display of the second and third time using the Timeout function, which still works?
(The answer suggested by this link below seems promising, but it doesn’t work for me. Maybe they don’t work on Bootstrap 3? How to automatically close bootstrap modal dialog in a minute )
This code below looked very promising, but didn’t work even after changing “shown” to “shown .bs.modal”. Or maybe I put this code in the wrong place?
var myModal = $('#myModal').on('shown', function () { clearTimeout(myModal.data('hideInteval')) var id = setTimeout(function(){ myModal.modal('hide'); }); myModal.data('hideInteval', id); })
Thanks so much for any suggestions!