Introduction to JavaScript Counter
The counter is one of the important components for using dashboards in the application. In real scenarios, it mainly uses the API called Pusher for creating and using the techniques called node.js application. With the help of these packages, we will create our own customized views, and also, it carefully listens to the changes, especially to the number of votes for an individual item.
Syntax and Parameters
In Javascript, the counter is one of the features for the UI interactions. It will be implemented in one or more ways like we have to initialize and declaring the count variables, and another way is we have to use Pusher API for counting the user-defined customized views on the web page.
<html>
<head>
<script>
var v= 60;
var v1 =setInterval(functionname,1000);
</script></head>
<body></body>
</html>
-Using the above codes, we will call some default methods for binding the datas without encryption. For each channel, the vote count will vary depending on the initialized voter variable in the script.
How is Counter done in JavaScript?
We can use many different APIs in the script, but every API has its features with both advantages and disadvantages. Pusher API is one of the apis it will use for real-world user counts for web-based applications. With the help of the methods,, we will store and fetch the datas in the databases. It’s one of the easiest methods for adding real-time functionalities to applications. The applications themselves typically handle this process. During this signup process, the applications generate and retrieve the necessary app credentials, such as an APP ID, Secret Keys, and other encryption and decryption data, from the designated “App Keys” sections.
Apart from that, javascript has some default plugins like scroll-triggered Counter Plugin, which comes under the purecounter.js because it’s based upon the user requirement for counting the datas in the UI. It also stores in the browser cache for temporary storage. We need to make two functions in the javascript. It takes the value of a text area element to calculate the total words, which will be displayed on the placeholder element.
Examples of JavaScript Counter
Following are the examples.
Example #1
Code:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function demo() {
var d = document.getElementById ("first");
if (window.getComputedStyle) {
var comp = window.getComputedStyle (d, null);
try {
var v = comp.getPropertyCSSValue ("content");
try {
varcvalue = null;
if (v) {
if (v.cssValueType == CSSValue.CSS_PRIMITIVE_VALUE) {
if (v.primitiveType == CSSPrimitiveValue.CSS_COUNTER) {
cvalue = value.getCounterValue ();
}
}
else {
if (v.cssValueType == CSSValue.CSS_VALUE_LIST) {
if (v.length> 0) {
if (v[0].primitiveType == CSSPrimitiveValue.CSS_COUNTER) {
counterValue = v[0].getCounterValue ();
}
}
}
}
}
if (cvalue) {
alert ("(identifier: " + cvalue.identifier +
", listStyle: " + cvalue.listStyle +
", separator: " + cvalue.separator + ")");
}
else {
alert ("The value of the counter-increment property is not a counter function!");
}
}
catch (e) {
alert ("Please find the error your browser does not support the getCounterValue method!");
}
}
catch (e) {
alert ("Please find the error your browser does not support the getPropertyCSSValue method!");
}
}
else {
alert ("Please find the error your browser does not support the getComputedStyle method!");
}
}
</script>
</head>
<body>
<div id="first" style="content: counter(c);">The html element is style="content: counter(c);"</div>
<br/>
<button onclick="demo()">Welcome To My Domain!</button>
</body>
</html>
Output:
Example #2
Code:
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
p {
display: inline;
font-size: 40px;
margin-top: 0px;
}
</style>
</head>
<body>
<p id="days"></p>
<p id="hours"></p>
<p id="mins"></p>
<p id="secs"></p>
<h2 id="end"></h2>
<script>
var c = new Date("Jul 28, 2022 22:24:16").getTime();
var mf = setInterval(function() {
var n = new Date().getTime();
var t = c - n;
var da = Math.floor(t / (1000 * 60 * 60 * 24));
var ho = Math.floor((t % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var mins = Math.floor((t % (1000 * 60 * 60)) / (1000 * 60));
var sec = Math.floor((t % (1000 * 60)) / 1000);
document.getElementById("days").innerHTML = da + "d "
document.getElementById("hours").innerHTML = ho + "h "
document.getElementById("mins").innerHTML = mins + "m "
document.getElementById("secs").innerHTML = sec + "s "
if (t < 0) {
clearInterval(mf);
document.getElementById("days").innerHTML = ""
document.getElementById("hours").innerHTML = ""
document.getElementById("mins").innerHTML = ""
document.getElementById("secs").innerHTML = ""
document.getElementById("end").innerHTML = "TIMES UP Please try new dates!!";
}
}, 1000);
</script>
</body>
</html>
Output:
Example #3
Code:
<!DOCTYPE HTML>
<html>
<body>
<div id="first"></div>
<script>
var tf = 60;
var e = document.getElementById('first');
var t = setInterval(demo, 1000);
function demo() {
if (tf == -1) {
clearTimeout(t);
second();
} else {
e.innerHTML = tf + ' Time seconds for remaining';
tf--;
}
}
function second() {
alert("Hi Welcome User");
}
</script>
</body>
</html>
Output:
Conclusion
Generally, JS is used for snippet actions. It will be available for all the scenarios. It increments the counter values automatically based on requirements given by the user. If we need to change the customized times, it will do the changes only in the scripts, even though we are not in touch with html codes.
Recommended Articles
This is a guide to JavaScript Counter. Here we also discuss the introduction and how the counter is done in javascript. along with different examples and code implementation. You may also have a look at the following articles to learn more –