0

I am trying to make a stop watch in js and my code is working correctly until I include Millisecond in my stopwatch.

Here's my code:

index.html

<html>
    <head>
        <title>My Second Page</title>
        <script src="script.js"></script>
    </head>
    <body>
        <input type="button" onclick="gettime()" value="Start Stopwatch"></a>
    </body>

</html>

script.js

 var ss = 0,
    mm = 0,
    hh = 0,
    ms = 0;
var once = 0;

function gettime() {
    if (once == 0) {
        document.write('<h1 id="stopwatch"></h1>');
        once++;
    }
    setInterval("msss()", 100);
}

function msss() {   // I have defined msss

    printtime();
    if (ms > 1000) {
        sec();
    } else {
        ms += 100;
    }

}

function sec() {
    ms = 0;
    printtime();
    if (ss > 59) {
        min();
    } else {
        ss += 1;
    }
}

function min() {
    ss = 0;
    printtime();
    if (mm > 59) {
        hr();
    } else {
        mm += 1;
    }
}

function hr() {

    mm = 0;
    printtime();
    hh += 1;
}

function printtime() {
    document.getElementById('stopwatch').innerHTML = 'H: ' + hh + 'M: ' + mm + ' S: ' + ss + ' MS: ' + ms;
}

Error Code

ReferenceError: msss is not defined

What am I doing wrong?

Ashesh Kumar
  • 223
  • 2
  • 12
  • 2
    shouldnt be setInterval(msss, 100); ? – Prabhu Murthy Aug 20 '14 at 17:03
  • @BOSS Thanks it works, But it doesn't works on Firefox 30.0 . Please help – Ashesh Kumar Aug 20 '14 at 17:05
  • 1
    Your `setInterval` call should look like this: `setInterval(msss, 100)` – Bojangles Aug 20 '14 at 17:06
  • 3
    Is that an input element closed with `` ? – adeneo Aug 20 '14 at 17:06
  • 1
    And, the code posted works just fine -> **http://jsfiddle.net/adeneo/0r2tpt77/**, as noted passing strings to `setInterval` is bad practice, but shouldn't break anything. – adeneo Aug 20 '14 at 17:07
  • @adeneo +1 for pointing that mistake! – Ashesh Kumar Aug 20 '14 at 17:08
  • 1
    `setInterval("msss()", 100);` is bad practice. Passing a string, makes it use `eval`, which you should not do. Use `setInterval(msss, 100);`. Also, don't use `document.write`. Use `document.body.innerHTML` instead. `document.write` will trash your entire page (erase it) and replace it with the string. It might've removed your JavaScript, thus causing the problem. – gen_Eric Aug 20 '14 at 17:09
  • @adeneo: Chrome handles `document.write()` differently than Firefox. Open that JSFiddle in Firefox. (Also, see http://stackoverflow.com/a/25398255) – gen_Eric Aug 20 '14 at 17:27
  • 1
    @RocketHazmat - yeah, `document.write` is always a bad idea, I only tested it quickly in Chrome, and it worked, but there where *many* things that could be improved. – adeneo Aug 20 '14 at 17:31
  • @RocketHazmat I am writing this for personal use, So its ok? right? – Ashesh Kumar Aug 20 '14 at 17:32
  • @AsheshKumar: I wouldn't suggest it. It's better to learn and use the correct practices always, than to get lazy on a simple project. That will help you do it the right way when it really does matter. – gen_Eric Aug 20 '14 at 17:32

2 Answers2

1

The problem is your document.write(). It's a bad practice to use this. What happens is, when this is called, the page is fully erased, and replaced with the code.

This removes everything on your page, including the JavaScript. So, mss no longer exists, since you just deleted it.

This was explained on an answer to a previous question of yours (https://stackoverflow.com/a/25398255).

Try something like:

document.body.innerHTML = '<h1 id="stopwatch"></h1>';
Community
  • 1
  • 1
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
0

How about:

setInterval(msss, 100);

That will execute your msss function every 100 miliseconds. Right now, you're passing in a string and not a function.

Jorge Silva
  • 4,574
  • 1
  • 23
  • 42