Home > Software engineering >  I want to delay a bs modal shows on page loads
I want to delay a bs modal shows on page loads

Time:12-31

 $(document).ready(function() {
     if (sessionStorage.getItem('#myModal') !== 'true') {
         $('#myModal').modal('show');
         sessionStorage.setItem('#myModal','true');     
     }
});

This is the code that I use for modal. I want to delay 1 second this modal on page load.

$(document).ready(function() {
  var delayMs = 2000; // delay in milliseconds
     if (sessionStorage.getItem('#myModal') !== 'true') {
         $('#myModal').modal('show');
         sessionStorage.setItem('#myModal','true');     
     }
 }); 

I tried this but it didn't work.

CodePudding user response:

It appears you intended to wrap some of your nested ready code in a setTimeout block but missed the actual implementation. See if it works with this:

 $(document).ready(function() {
   var delayMs = 2000; // delay in milliseconds
   setTimeout(() => {
     if (sessionStorage.getItem('#myModal') !== 'true') {
         $('#myModal').modal('show');
         sessionStorage.setItem('#myModal','true');     
     }
   }, delayMs);
 }); 
  • Related