Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

330
Views
How to use setInterval in Javascript [Chrome Extension] development

I'm learning how to make chrome extensions and the project that I'm working on is a 'Clock' in a new tab.

Issue: I'm able to retrieve the current time, but I'm unable to refresh the page every second. How can I fix this?

main.js

setInterval(function () {
  var d = new Date();

  const h = d.getHours();
  const m = d.getMinutes();
  const s = d.getSeconds();

  // Current Time w/ format
  const cFormat = h + ":" + m;

  const cTime = document.getElementById("cTime");
  cTime.textContent = cFormat;
}, 1000);

manifest.json

{
    "manifest_version": 3,
    "name": "ClockTab",
    "description": "Clock.",
    "version": "1.0.0",
    "persistent": true,
    "icons": {"128": "icon_128.png"},
    "host_permissions": ["<all_urls>","activeTab","tabs"],
    "action" : {
        "default_popup": "popup.html",
        "default_icon": "icon_128.png"
    },
    "chrome_url_overrides" : {
        "newtab": "index.html"
    },
    "background": {
        "service_worker": "main.js",
    },
    "content_scripts": [{
        "css": ["style.css","popup.css"],
        "js": ["jquery.min.js", "main.js", "popup.js","dateTime.js"],
        "all_frames": true,
        "matches": ["http://*/*"]
    }]
}

index.html

<div id="cTime"></div>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I opted for setTimeout allowing for the clock to refresh every second which only occurs after the page loads.

main.js

function start() {

  // Clock
  function getTime() {
    var d = new Date();

    const h = d.getHours();
    const m = d.getMinutes();
    const s = d.getSeconds();

    // Current Time w/ format
    const cFormat = h + ":" + m;

    const cTime = document.getElementById("cTime");
    cTime.textContent = cFormat;

    setTimeout(function () {
      getTime();
    }, 1000);
  }

  getTime();
}

window.addEventListener("load", start);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!