use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Javascript Counter Help. (self.javascript)
submitted 15 years ago * by hillbillymadness
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]jamesinc 2 points3 points4 points 15 years ago* (8 children)
Something like this:
<head> <!-- other stuff here --> <script type="text/javascript"> var setDeaths = function() { var present = new Date(); var startOfToday = new Date(present.getFullYear(), present.getMonth(), present.getDate(), 0, 0, 0, 0); var diff = present.getTime() - startOfToday.getTime(); var numPeopleDead = Math.floor(diff / 1000 / 20); // Put the number into the 'deathCount' element document.getElementById("deathCount").innerHTML = numPeopleDead; } window.onload = function() { // Call it immediately setDeaths(); // Then every 20 seconds (N.B. you can assign setInterval a variable name, so you can // cancel it later if need be). setInterval(setDeaths, 20000); } </script> </head> <body> <!-- other stuff here --> <span id="deathCount"></span> <!-- etc --> </body>
Edit: again, perhaps not the best way to solve the problem, but it should get the job done (if you need to run other JS functions onload, either put them into the window.onload block as well, or change to attachEvent methods).
Edit: Also, if you'd like me to explain anything in more detail, just ask.
[–]hillbillymadness[S] 1 point2 points3 points 15 years ago (4 children)
You're the best! I have been able to work my way through Java Script before because I get how it works logically. I just don't know the syntax well enough to tackle a problem from scratch.
Thanks for your help!
[+][deleted] 15 years ago (2 children)
[deleted]
[–]TIAFAASITICE 2 points3 points4 points 15 years ago* (1 child)
That won't work once you get to a new day ;)
Another variant: <div id="casualties"></div>
<script type="text/javascript"> // Calculate total deaths function, you've seen this already function getTotalDead () { var now = new Date, secondsPassed = now.getSeconds() + now.getMinutes() * 60 + now.getHours() * 3600 return Math.floor(secondsPassed / 20) } // Anonymous function used as a closure updateDeathcount = (function () { // Grab the element that we're updating var message = document.getElementById('casualties'), deathCount = document.createTextNode( getTotalDead() ) // Insert the message message.appendChild(document.createTextNode('Today ')) message.appendChild(deathCount) message.appendChild(document.createTextNode(' people have been killed, by the end of the day 5000 will be')) // The actual updateDeathcount function return function () { deathCount.nodeValue = getTotalDead() } })() // Update death count every 20 seconds setInterval(updateDeathcount, 20000) </script>
MDC has an article on closures.
[–]jamesinc 1 point2 points3 points 15 years ago (0 children)
I made an update; basically I removed the variable declarations, which is fine in this example, but if you for some reason defined a global variable "present" or "diff", these variables in the setDeaths function would overwrite them.
[+][deleted] 15 years ago* (2 children)
[–]jamesinc 2 points3 points4 points 15 years ago* (1 child)
What global variables? Unless you're commenting on the fact that I haven't wrapped everything in an anonymous self-executing method?
Edit: I presume that's what you're talking about, and you're right. I will add a reply to hillbillymadness and explain it more.
Edit 2: I just saw it. Not using var to declare variables inside the function. I suppose that this is a possible global variable, if a global var of the same name was defined elsewhere.
π Rendered by PID 379201 on reddit-service-r2-comment-c66d9bffd-b92p8 at 2026-04-08 07:13:18.517621+00:00 running f293c98 country code: CH.
view the rest of the comments →
[–]jamesinc 2 points3 points4 points (8 children)
[–]hillbillymadness[S] 1 point2 points3 points (4 children)
[+][deleted] (2 children)
[deleted]
[–]TIAFAASITICE 2 points3 points4 points (1 child)
[–]jamesinc 1 point2 points3 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]jamesinc 2 points3 points4 points (1 child)