Javascript code does not update

I recently added an update to my clock app which adds milliseconds to the clock. But the JavaScript code has not updated.

Code on Glitch:

function startTime() {
  const today = new Date();
  let h = today.getHours(); 
  let m = today.getMinutes();
  let s = today.getSeconds();
  let ms = today.getMilliseconds();
  h = checkTime(h);
  m = checkTime(m);
  s = checkTime(s);
  ms = checkTime(ms)
  document.getElementById('time').innerText =  h + ":" + m + ":" + s ;
  setTimeout(startTime, 1000);
}

function startMs() {
  const today = new Date();
  let ms = today.getMilliseconds();
  document.getElementById('ms').innerText = ms
  setTimeout(startMs, 1)
}

function checkTime(i) {
  if (i < 10) {i = "0" + i};  // add zero in front of numbers < 10
  return i;
}

startTime()

Code on Website:

/*
This is your site JavaScript code - you can add interactivity and carry out processing
- Initially the JS writes a message to the console, and moves a button you can add from the README
*/

// Print a message in the browser's dev tools console each time the page loads
// Use your menus or right-click / control-click and choose "Inspect" > "Console"
console.log("Hello 🌎");

function startTime() {
  const today = new Date();
  let h = today.getHours(); 
  let m = today.getMinutes();
  let s = today.getSeconds();
  h = checkTime(h);
  m = checkTime(m);
  s = checkTime(s);
  document.getElementById('time').innerText =  h + ":" + m + ":" + s ;
  setTimeout(startTime, 1000);
}



function checkTime(i) {
  if (i < 10) {i = "0" + i};  // add zero in front of numbers < 10
  return i;
}

startTime()


  
}

Is there any way to brute update the app?

By “website” do you mean the page that you see in your browser? It may simply be that the page is cached. Clear the cache of your website and you should get the latest version. Good way to confirm it (if you want to) is to use another browser that you haven’t visiting the site with.

You may have a small cosmetic bug in the milliseconds display as it should ordinarily be 3 positions but I don’t know if you want 001, etc. for that part.

You can also do some interesting things with nested functions, arrow functions and string tnterpolation. And perhaps setInterval would be a better choice.

function clock() {

	const pad0 = (num) => ('0' + num.toString()).substr(-2, 2);
	const pad00 = (num) => ('00' + num.toString()).substr(-3, 3);

	const nowStr = (now) => `${pad0(now.getHours())}:${pad0(now.getMinutes())}:${pad0(now.getSeconds())}:${pad00(now.getMilliseconds())}`;
  
  	document.getElementById('time').innerText =  nowStr(new Date());
}

setInterval(clock, 1000);

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.