Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show modal after every 5 seconds

Tags:

javascript

I want my modal to show as a popup after every 5 seconds without triggering the button, but I am unable to do it. I used jquery function show and setTimeout but it works only the first time the page is loaded

This is my jquery:

<script>
$(document).ready(function(){
   setTimeout(function(){
       $('#myModal').modal('show');
   }, 2000);
});
</script>

My Modal:

<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">

      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">
          <p>Some text in the modal.</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>

    </div>
  </div>

I want it to open after every 5 seconds Please help

like image 548
juhi Avatar asked Dec 06 '25 04:12

juhi


1 Answers

Try this

setInterval(function () {
   $('#myModal').modal('show');
}, 5000);

The setInterval() method will continue calling the function until clearInterval() is called, or the window is closed.

like image 90
Chintan Joshi Avatar answered Dec 07 '25 18:12

Chintan Joshi