1

In my javascript

$(document).ready(function() {
    console.log((new Date().getTime() / 1000));
      if ((new Date(setting.lastDate).getTime() / 1000) <= (new Date().getTime() / 1000)) {
        $('.Go').removeAttr("disabled");
        $( ".headertesting").replaceWith(" ");
        $( ".testing" ).replaceWith( "<span class='butlabel testing' >Register Now!</span><span class='spinner'></span>" );
      }
});

In my html when i run this function . It's not worked as i expected I have a countdown date: Example i set

lastDate = "06/01/2016 10:21:00"

So it will check my currentTime and compare it. But when it reach. the button didn't update directly. I have to refresh the page only see the result. What i want is directly button change without refresh page once time reached.

philo
  • 3,580
  • 3
  • 29
  • 40
D2P coding
  • 11
  • 1
  • 1
    You will probable want to check out setTimeout() – JonSG May 31 '16 at 02:37
  • not working using timeout – D2P coding May 31 '16 at 03:07
  • "not working" is the LEAST useful thing a person can say about a bit of code. Show the code you've tried and explain what, *exactly*, is not working. `setTimeout` or `setInterval` **is** the correct tool to use. Update your question with a snippet that shows the problem you are having. Include the needed HTML. – Jeremy J Starcher May 31 '16 at 04:59

2 Answers2

0

That's because $(document).ready() executes only once, when the page loads. You're probably expecting that your function executes repeatedly, maybe every second or so, so that it updates as soon as the timer reaches that condition. Try window.setTimeout or window.setInterval. Here's a related question with a good answer: What's the easiest way to call a function every 5 seconds in jQuery?.

Community
  • 1
  • 1
philo
  • 3,580
  • 3
  • 29
  • 40
  • It's more helpful for others on stackoverflow if you ask this as a new question. Be sure to say what you did (show code, like you did on this question), what you expected to happen, and what actually happened. – philo May 31 '16 at 16:24
0

When you say "is not working", what did you try?

var resultsEl = document.getElementById("time");

window.setInterval(function(){
  resultsEl.innerText = new Date().toLocaleTimeString();
}, 1000);
<div id="time" style="height: 1em; background-color: aliceblue;"></div>
JonSG
  • 10,542
  • 2
  • 25
  • 36