[Javascript] I did a timer using classes and have some issues
Hey you guys, I'd like to please about rate my code (especially javascript) rate, what should i change, what I did wrong and etc.
While it counts font isn't same size all the time, I mean the font slightly moves in different directions (like i screwed up something in css but i don't know)
My code is too long so I think i can upload it on pastebin
https://pastebin.com/p20rSRKE
Pastebin
My code has some issues - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
2 Replies
so a couple of quick fixes
1. dont couple the class to a specific dom node.
add an option inside the constructor to feed it in.
I also like to make sure that everythings loaded before firing off my functions
2. to stop the jumping that is caused by numbers being slightly different widths we can add some minor styling
your code is definitely not too long and it fits easily into a codepen
https://codepen.io/CA-Carl/pen/dyrvXNG?editors=1111
Instead of checking manually if the number is less than 10 and adding an additional 0, you can use the
padStart
function:
You are also not re-assigning this formattedTime variable anywhere, so use const
instead. As a general, rule, declare variables with const
first and only use let
when you do need to re-assign them. It's slightly more expressive that way.
I would also like to see a more consistent naming of methods. For example "timer" to start, why not call it "start"? Also with "nextFunc", doesn't mean anything. It'd be better to use descriptive names so that I don't have to go to the definition of that function to understand what it does. I only want to read that code if I want to understand how it does it.