MomentJS only showing time on load

Viewed 168

Using momentjs to get current times, however, I'm noticing its only getting the values during load. What is causing this here? My suspicion is text() is not proper for a dynamic value?

var zone = [
    est = 'America/New_York',
    cen = 'America/Mexico_City',
    mount = 'America/Denver',
    pac = 'America/Los_Angeles'
]
var est = moment.tz(moment(), zone[0]).format('HH:MM:ss a');
var cen = moment.tz(moment(), zone[1]).format('HH:MM:ss a');
var mount = moment.tz(moment(), zone[3]).format('HH:MM:ss a');
var pac = moment.tz(moment(), zone[4]).format('HH:MM:ss a');

$("#est").text(est)
$("#pac").text(pac)
$("#cen").text(cen)
$("#mount").text(mount)

Example of dom elements:

        <div class="clock">
            <p>Pacific</p>
            <p id="pac" class="time">1:00 PM</p>
        </div>
        <div class="clock">
            <p>Mountain</p>
            <p id="mount" class="time">1:00 PM</p>
        </div>
1 Answers

moment() gives you the current time once, and that's all. You need to update the time (for example, every second) yourself. Also, $(...).text updates the text content just once.

Try creating an interval:

window.setInterval(() => {...code to execute...}, 1000);
Related