13

I have this function, which I would like to be executed after 5 seconds:

$(document).ready(function() {
   $("#signInButton").trigger('click');

  });

Thank you

0

4 Answers 4

44

Use setTimeout() function:

$(document).ready(function() {
  setTimeout(function() {
    $("#signInButton").trigger('click');
  }, 5000);
});
Sign up to request clarification or add additional context in comments.

2 Comments

thank you I have one comment, In addition to loading after 5 seconds, how would i work it so that it only load one every 2 hours and not per page.As of right now the modal appears every time the page is load, but i would like to load every 2 hours that page is load, so if i load the page now i see it, but if i load it 10 minute later i don't see it but if load this page again in 2 hours i see it or per browser session
You could probably could achieve that with cookies (w3schools.com/js/js_cookies.asp). Store the server dateTime in a cookie and compare the last saved dateTime every time you load the page. If the new dateTime differs more than 2 hours, execute your function.
5

Use setTimeout()

$(document).ready(function() {
  setTimeout(function() { 
    $("#signInButton").trigger('click');
  }, 5000); // for 5 second delay 
});

2 Comments

@JonathanEtienne - see the answer - you have to use SetTimeout()
thank you I have one comment, In addition to loading after 5 seconds, how would i work it so that it only load one every 2 hours and not per page.As of right now the modal appears every time the page is load, but i would like to load every 2 hours that page is load, so if i load the page now i see it, but if i load it 10 minute later i don't see it but if load this page again in 2 hours i see it or per browser session
1
$(document).ready(function() {
    setTimeout(function() {
        $("#signInButton").trigger('click');
    }, 5000);
});

2 Comments

You might want to describe why this is the correct answer.
thank you I have one comment, In addition to loading after 5 seconds, how would i work it so that it only load one every 2 hours and not per page.As of right now the modal appears every time the page is load, but i would like to load every 2 hours that page is load, so if i load the page now i see it, but if i load it 10 minute later i don't see it but if load this page again in 2 hours i see it or per browser session
1

Something like this ?

$(document).ready(function() {
  setTimeout(function() { 
    $("#signInButton").trigger('click');
  }, 5000);

  $("#signInButton").click(function(){
    alert("I'm clicked!");
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" id="signInButton" value="Click Me" />

Learn more about window's setTimeOut method

2 Comments

thank you I have one comment, In addition to loading after 5 seconds, how would i work it so that it only load one every 2 hours and not per page.As of right now the modal appears every time the page is load, but i would like to load every 2 hours that page is load, so if i load the page now i see it, but if i load it 10 minute later i don't see it but if load this page again in 2 hours i see it or per browser session
@JonathanEtienne, you need to describe more on what you exactly want to achieve, you may use setInterval() that will call your function periodically i.e. every 10 mins or something, but the way you're asking like every 2 hours it should call some function, I don't think it will be good to use JavaScript because, in between that interval if user refresh the page then your script will be reset. Read more about setInterval() here, w3schools.com/jsref/met_win_setinterval.asp