variables in a function not updating constantly when called on node side

I have a function called startTime() which gets the current time with h set to the hour and m set to minutes. The error with this code is after I exported my javaScript side function to node with the specific variables when I call back h and m their values don’t get updated with the startTime function they are stuck to the values they first had when I run startTime in the javascript side. I tried to setInterval to keep on calling startTime every 500 milliseconds but that didn’t fix it.function refreshStartTime() {setInterval(startTime, 500);} .I also tried debugging it with an alert on the HTML side which printed the correct/constantly updating values of h and m.“`function testAlert(){alert(h + “:” + m)} .Do you have any idea why it’s not updating on the node side or how to solve this issue? Thanks in advance.

JavaScript side

function startTime() {
    var today = new Date();
    globalThis.h = today.getHours();
    m = today.getMinutes();
    var s = today.getSeconds();
    globalThis.m = checkTime(m);
    s = checkTime(s);
    var t = setTimeout(startTimeAudio, 500);
  }
  function checkTime(i) {
    if (i < 10) {i = "0" + i};  // add zero in front of numbers < 10
    return i;
  }
 
startTime() 

module.exports = {
    startTime,
    h,
    m,
    
};

Node side

const qw = require('./index.js');

const timeH = qw.h;
const timeM = qw.m;

Answer

Change your module.exports to

module.exports = {
  startTime,
  ...globalThis
}

Leave a Reply

Your email address will not be published. Required fields are marked *