This lightweight JavaScript code snippet helps you to create spacebar hit counter. It is based on a simple idea to count the spacebar hits. Simply, it executes the keyup function, match the spacebar keycode, and increase the hit value using increment operator.
Whether you’re working on a game or web app in which you need to count spacebar hits, this simple code snippet equally helpful.
How to Create Spacebar Hit Counter in JavaScript
1. First of all, create a span element with a class name “hits” that will holds the number of hits counted by JS function. Similarly, create a button or anchor link with onclick attribute to register a click event for resetHits() function. Also, define a class name “tryagain” for the button, it will be used to reset the counting variable. The complete HTML structure for the counter is as follows:
<div id="activity"> <h1 id="counter">You have hit the spacebar <span class="hits">0</span> times.</h1> <a href="#" onclick="resetHits()" class="tryagain">RESTART</a> </div>
2. Add the basic CSS style for the hits counter. Basically, these styles are optional, you can go without them.
body { margin: 0; font-family: 'Open Sans', sans-serif; position: absolute; width: 100vw; height: 100vh; overflow: hidden; display: table; } #activity { display: table-cell; text-align: center; vertical-align: middle; } #result { text-transform: uppercase; } .tryagain { background-attachment: scroll; background-clip: border-box; background-color: rgb(127, 206, 119); background-image: none; background-origin: padding-box; background-size: auto; border-bottom-left-radius: 16px; border-bottom-right-radius: 16px; border-top-left-radius: 16px; border-top-right-radius: 16px; box-sizing: border-box; color: rgb(255, 255, 255); cursor: pointer; display: inline-block; font-family: 'Open Sans', sans-serif; font-size: 32px; font-stretch: normal; font-style: normal; font-variant: normal; font-weight: normal; height: 93px; line-height: normal; margin-bottom: 8px; margin-left: 9.28px; margin-right: 9.28px; margin-top: 8px; min-height: 0px; min-width: 208px; outline-width: 0px; padding-bottom: 24px; padding-left: 24px; padding-right: 24px; padding-top: 24px; position: relative; text-align: center; text-transform: none; transition-delay: 0s; transition-duration: 0.28s; transition-property: box-shadow; transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1); width: 351.594px; z-index: 0; -webkit-user-select: none; } a:link, a:hover, a:visited, a:active { text-decoration: none; } .hits { font-size: 2em; font-weight: bolder; }
3. Finally, create a variable with initial value and get the span element by using JavaScript query selection. Use the on keyup function and check if key is a spacebar. The key code for spacebar is 32. So, use if statement to detect if spacebar has been pressed then call the addHit() function.
Define addHit() function and simply use increment operators to increase the hits variable value.
Add the following script between the <script> and </script> tag place it before the closing the body tag.
var hits = 0; var hitElement = document.querySelector( '.hits' ); document.body.onkeyup = function(e) { if( e.keyCode == 32 ) { addHit(); } } var addHit = function() { hits++; renderHits(); } var renderHits = function() { hitElement.innerHTML = hits; } var resetHits = function() { hits = 0; renderHits(); }
That’s all! hopefully, you have successfully created Spacebar Hit Counter Javascript Code. If you have any questions or suggestions, feel free to comment below.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.