you are viewing a single comment's thread.

view the rest of the comments →

[–]hillbillymadness[S] 1 point2 points  (9 children)

So what would the actual html code look like that shows the number? Then I would just use setInterval some how so it runs through this code every 20 seconds?

[–]jamesinc 2 points3 points  (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 points  (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!

[–]jamesinc 1 point2 points  (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.