0

I am new to Chrome extensions, but I have been trying to learn how to create one. Right now, I am working on a very simple extension that times how long you spend on your computer (or more specifically the Chrome web browser).

chrome extension timer

I managed to get the timer working on the extension's popup, but every time I close the popup and reopen it the timer resets to 0. I have been trying to get the timer to keep running in the background but nothing seems to be working. Below is my background.js file.

var time = 0;

function start() {
  increment();
}


function increment() {

  setInterval(function() {

    time++;
    var mins = Math.floor(time/10/60);
    var secs = Math.floor(time/10);

    if (mins < 10)
      mins = "0" + mins;
    if (secs < 10)
      secs = "0" + secs;

    document.getElementById("time").innerHTML = mins + ":" + secs;

  }, 100);
}


start();

All I really want is the timer to keep running even when the popup is closed.

Makyen
  • 31,849
  • 12
  • 86
  • 121
ad562
  • 1
  • 1
  • You'll need to post your `manifest.json`. There's a difference between a popup script and a background script (and an event script). – Teepeemm Jan 29 '17 at 03:39
  • Please [edit] the question to be on-topic: include a **complete** [mcve] that *duplicates the problem*. Including a *manifest.json*, some of the background/content/popup scripts/HTML. Questions seeking debugging help ("**why isn't this code working?**") must include: ►the desired behavior, ►a specific problem or error *and* ►the shortest code necessary to reproduce it **in the question itself**. Questions without a clear problem statement are not useful to other readers. See: "**How to create a [mcve]**", [What topics can I ask about here?](http://stackoverflow.com/help/on-topic), and [ask]. – Makyen Jan 29 '17 at 04:47
  • I would suggest that you read the [Chrome extension overview](https://developer.chrome.com/extensions/overview) (and perhaps along with the pages linked from the overview). The [architecture section](https://developer.chrome.com/extensions/overview#arch) has overall architecture information which should help your understanding of how things are generally organized/done. – Makyen Jan 29 '17 at 04:50
  • 1
    Possible duplicate of [Completely lost on how to save Chrome extension popup window content](http://stackoverflow.com/questions/41284528/completely-lost-on-how-to-save-chrome-extension-popup-window-content) – Makyen Jan 29 '17 at 05:00
  • You are going to need to store the time that your timer started outside of the popup using one of the methods described in the duplicate. You will then need to display the difference between the start time and the current time, not by keeping an incremental count of seconds. – Makyen Jan 29 '17 at 05:10

0 Answers0