OGDigital
OGDigital

Reputation: 43

How to delay JS function from running until 5 seconds after page loads

I want this to run but so that it doesn't for 5 seconds after the page fully loads. How would I go about achieving this, I believe its a ,500 somewhere but I am not sure where this would go.

If you have any questions please ask!

Thank you in advance for you help on this matter, its very much appreciated!

    $(".demoBookedContentClose").click(function(){
  $("body").addClass("demoBookedHidden");
});


    function shuffle(array) {
  var currentIndex = array.length,
    temporaryValue, randomIndex;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}

var queue = [];

        
        
function setUp() {
  var elems = $(".demoBooked").get();
  queue = shuffle(elems);
  showNext();
}

function showNext() {
  var elem = queue.pop();
  if (elem) {
    $(elem)
      .fadeIn(2000)
      .delay(5000)
      .fadeOut(1000, function(){ setTimeout(showNext,25000); });
  } else {
    setUp();
  }
}

setUp();
    .demoBooked {
    background: #fff;
    box-shadow: 0 1px 2px rgba(0,0,0,0.05), 0 2px 4px rgba(0,0,0,0.08);
    border: 1px solid #dddddd;
    padding: 20px;
    border-radius: 8px;
    display: none;
    }
    .demo-booked-section {
    position: fixed;
    bottom: 10px;
    left: 10px;
    z-index: 2;
    }
    .demoBooked h3 {
    font-size: 22px;
    font-weight: 900;
    margin-bottom: 4px;
    }
    .demoBooked img {
    max-width: 50px;
    margin-top: -55px;
    border-radius: 100%;
    display: inline-block;
    }
    .demoBookedContent {
    display: inline-block;
    padding-left: 20px;
    }
    .demoBooked p {
    font-size: 18px;
    line-height: 20px;
    }
    .demoBookedTime {
    color: #e12826;
    }
    .demoBookedContentClose {
    position: absolute;
    right: 15px;
    top: 10px;
    font-size: 10px;
    cursor: pointer;
    }
    .demoBookedHidden .demo-booked-section {
    display: none!important;
    }
    .demoBookedTime {
        font-size: 15px;
    }
    @media only screen and (max-width: 768px) {
        .demo-booked-section {
            display: none!important;
        }   
    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="demo-booked-section">
    <div class="demoBooked">
        <img src="/wp-content/uploads/2021/01/william-diaz.jpg">
        <div class="demoBookedContent">
            <span class="demoBookedContentClose">X</span>
            <h3>William Diaz</h3>
            <p class="demoBookedText">Just started a FREE trial</p>
            <p class="demoBookedTime">1hrs ago</p>
        </div>
    </div>
    <div class="demoBooked">
        <img src="/wp-content/uploads/2021/01/freya-smith.jpg">
        <div class="demoBookedContent">
            <span class="demoBookedContentClose">X</span>
            <h3>Freya Smith</h3>
            <p class="demoBookedText">Just started a FREE trial</p>
            <p class="demoBookedTime">3hrs ago</p>
        </div>
    </div>
  </div>

Upvotes: 1

Views: 5899

Answers (3)

Arianne
Arianne

Reputation: 507

You can use setTimeout which will wait the specified number of milliseconds before running whatever is between the curly braces (in this case I put your setUp function there because I assume that's what you meant by your question).

Because you want the function to run 5 seconds after the page is fully loaded you need to surround the code in a call to window.onload which will make sure that setTimeout only runs once the page has been fully loaded.

window.onload = function() {  
   setTimeout(function() { setUp(); }, 5000);
}

Upvotes: 0

Abdush Adil
Abdush Adil

Reputation: 146

Since you want the function to be fired up 5 seconds after the page is fully loaded you will be using a combination of two functions.

I see you are using jQuery in your website The below code waits until the page is fully loaded then fires up the code inside the brackets.

$( document ).ready(function() {
     // code here
});

So inside the above code you will add your 5 seconds waiting function

setTimeout(function(){
    // Magic happens here
},5000); 

The final code is

$( document ).ready(function() {
    setTimeout(function(){
        // Magic happens here
    },5000); 
});

Upvotes: 0

JTCon
JTCon

Reputation: 509

You can delay your function 5 seconds (5000 ms) with an 'setTimeout' functions after your web load:

<script>

window.onload = function() {       
    setTimeout(function(){
        //Your function here
    },5000); 
}

</script>

Upvotes: 3

Related Questions