Tip for creating a better Wordpress Blog Web design.

If you are starting in the blogging world or already have experience with it, you know that having a good design could be a point for attracting more visitors and retain them in your website. If you…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Creating a simple timer in vanilla Javascript

I wanted to share with you all an easy and fun way to create a simple timer using Javascript (ES6) and HTML.

Here’s my HTML5 file:

Here, I have a very simple counter that starts at zero. I’ve created an h1 tag with an id of timer for easy finding when I start writing my javascript, as well as a button that I’ll use to pause my timer.

Ugly — I know. But it’s going to work and that’s what’s important for now! First thing I need to do is create a function, called timer(). When this function runs, the timer will go up by one second. When I press the pause button, it’ll stop at that number.

This means I’ll need to put in the argument of the function that I’ll need it to do, and how often I’ll need to do it in milliseconds. I want my timer to increment by 1 each second, so I’ve used the built in ++ from JS and an argument of 1000 milliseconds. YAY! We have a working timer:

Lastly, let’s get that pause button working. I’ve got the correct element selected in my HTML, so I’m going to add an eventListener to the variable pauseButton. Using the JS builtin, clearInterval(), I’m able to stop that timer every time the button is pressed!

Hope this was helpful to all the beginner programmers out there writing vanilla Javascript. Go forth and code!

Add a comment

Related posts:

Mom and Me

Because I accepted my position in life, I made money and it was never an issue until now and I told you before that I am in the winning team and yet, it feels like a hollow victory. It is a simple…

The Prettiest Smile

The way she walked down that grassy lane. “The Prettiest Smile” is published by Kathrine Lamar.

Revelations of My Phylogeny

In this year where I ceased to be released and free from the inconvenient confines of my mind I seek a space and time a near relation to the revelation of my phylogeny from student to scholar to…