+ 3
Stopwatch in javascript using interval function?
Hi guys, I was trying to creat a primitive stopwatch that counts until a certain point. All good so far, however I would like to see the new value (x++) to overwrite the previous value, not being added after it (1 2 3 4 5 ...)==> meaning there would always be only ONE number on the screen that changes periodically, like with a normal stopwatch. How could I achieve this? (Bonus question: how could I create a button that starts the counting?) https://code.sololearn.com/WB4W6cKb5P47/?ref=app
2 odpowiedzi
+ 9
Hi Mark, great start. I can see you are about 75% through JavaScript course which means you must be up to or very close to the DOM (Document Object Model) section. This is the section you will need to achieve what your trying.
As a start you will need:
1. An element in your HTML; suggest a <p></p> (paragraph). This will hold the value of 'x' which you will update via JavaScript rather than just writing to the screen (which is awesome for debugging). Note: if you give it an id, it's slightly easier to work with in your script.
2. JavaScript; you will use this to get the element. Presuming you gave it an id:
var myElement = document.getElementById("idhere");
3. Continue your JavaScript to modify.
Once you've read the DOM section of the course try this and let us know how you go. Hopefully you take this approach as I believe you will learn the DOM concepts better than if I just gave you all the code 😎
+ 1
Thank you! I am just a bit too impatient with coding, I cannot wait to find out the practical use of all the stuff I learned so far!