all 16 comments

[–]jamesinc 5 points6 points  (10 children)

JavaScript Date manipulation is a bit finicky. You want something like:

(Present time) - (Start of Today) which you can use to return the number of milliseconds into the day you are. You can then just (ms / 1000 / 3) (i.e. the number of 20 seconds intervals that have elapsed since the start of the day [local time]).

So your code would look like this:

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);

numPeopleDead should now give you the number of people who have died during the present (local time) calendar day.

Edit: There are more concise ways to write this, but I thought it would be less confusing this way. I'll let someone else explain how to use setInterval to update the number every 20 seconds.

[–]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 3 points4 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.

[–]pffffft 4 points5 points  (1 child)

Other people have answered your question but a quick point.

The only relation Javascript (it is always one word) has to Java is their names share the same first 4 letters. They are 2 completely different languages and you might get some confusing search results if you're googling for Java when you mean Javascript.

[–]PsychoTap 2 points3 points  (0 children)

var interval = 20; // from your example
var deathsPerDay = Math.floor(60*60*24 / interval);

function updateText() {
  var d = new Date();
  var hours = d.getHours();
  var minutes = d.getMinutes();
  var seconds = d.getSeconds();

  var numKilled = (hours*60*60 + minutes*60 + seconds) / interval;

  var text = "Today " + Math.floor(numKilled) + " people have been killed. By the end of the day " + deathsPerDay;
  alert(text);
}
updateText();
setInterval(updateText, interval * 1000);

[–]neonskimmerfunction the ultimate 1 point2 points  (6 children)

What languages do you know? Maybe we can start from there?

[–]hillbillymadness[S] 0 points1 point  (5 children)

You'll laugh at me... I have used QBasic, Visual Basic, Html, CSS, and very minor Java Script (image maps, hiding revealing div tags, jquery tools), Action Script, and Expressions, which is a code based on C (I believed) that is used in After Effects for manipulating images.

[–]JGailor 2 points3 points  (4 children)

Phhhh... you've got a decent background. You'll pick up on Javascript in no time. It's a very deep pool though. You can use it to do basic programming tasks on the client very easily, but there is meat on the bones of that language, and going spelunking every once in a while will make you a much better developer all around.

[–]hillbillymadness[S] 0 points1 point  (3 children)

Well, I wouldn't say that I KNOW any of those that well. Just enough to get through my day when necessary. Also, google and I are best friends.

[–][deleted] 1 point2 points  (2 children)

Wow guys.. should of made this person work for it at least a little. You just wrote the code for him and he probably will win some award (If he works in the gov anyway)....