0

I want a div reloads its time showed every 5 seconds.

JQUERY This is an approach...

time_showed1=$.now();//how to format in segs, then mins, then hours..

setInterval(function() { time_showed=time_showed1;}, 5000);

("#container").append("updated at:"+time_showed+" time");

HTML

  <div id="container"></div>

I tried this as well but nothing

myFunction = function() { 

        time_showed=$.now();
    };


      setInterval(myFunction(), 2000);
Dan
  • 517
  • 1
  • 3
  • 13
  • 1
    You need to update the HTML *inside* your setInterval callback if you want the HTML to update every time the interval is called – mhodges Mar 16 '18 at 15:24
  • Move your append inside the set interval function. you may also want to use .text instead of append - otherwise you get a new line of text every 5 seconds – Pete Mar 16 '18 at 15:25
  • what if onlywant to update an `span` within `#container`. Because I dont want to reload the whole container – Dan Mar 16 '18 at 15:26
  • 1
    target the span in your selector then – Pete Mar 16 '18 at 15:27
  • @joe `$(".my-span").text("updated at:" + time_showed + " time");` – mhodges Mar 16 '18 at 15:29
  • it make sense. let me try – Dan Mar 16 '18 at 15:30

1 Answers1

0
setInterval(function() { 
    time_showed = time_showed1;
    ("#container").append("updated at:" + time_showed + " time");
}, 5000);

You need to set the time inside the function called by setInterval. And

myFunction = function() {     
    time_showed=$.now();
    ("#container").append("updated at:" + time_showed + " time");
};
setInterval(myFunction, 2000);

You need to pass a function to setInterval, you were passing the result.

Yuriy Faktorovich
  • 67,283
  • 14
  • 105
  • 142