13

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

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

  });

Thank you

Jonathan Etienne
  • 621
  • 3
  • 6
  • 18

4 Answers4

42

Use setTimeout() function:

$(document).ready(function() {
  setTimeout(function() {
    $("#signInButton").trigger('click');
  }, 5000);
});
kapantzak
  • 11,610
  • 4
  • 39
  • 61
  • 1
    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 – Jonathan Etienne Apr 16 '15 at 17:51
  • You could probably could achieve that with cookies (http://www.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. – kapantzak Apr 17 '15 at 06:08
6

Use setTimeout()

$(document).ready(function() {
  setTimeout(function() { 
    $("#signInButton").trigger('click');
  }, 5000); // for 5 second delay 
});
prog1011
  • 3,425
  • 3
  • 30
  • 57
  • @JonathanEtienne - see the answer - you have to use SetTimeout() – prog1011 Apr 16 '15 at 13:06
  • 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 – Jonathan Etienne Apr 16 '15 at 17:51
2
$(document).ready(function() {
    setTimeout(function() {
        $("#signInButton").trigger('click');
    }, 5000);
});
graycodes
  • 373
  • 2
  • 13
  • 1
    You might want to describe why this is the correct answer. – rfornal Apr 16 '15 at 13:24
  • 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 – Jonathan Etienne Apr 16 '15 at 17:51
2

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

Mox Shah
  • 2,967
  • 2
  • 26
  • 42
  • 1
    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 – Jonathan Etienne Apr 16 '15 at 17:51
  • @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, http://www.w3schools.com/jsref/met_win_setinterval.asp – Mox Shah Apr 17 '15 at 04:54