Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimization suggestions for setInterval code that display how many puppies, kittens, etc. are born every second
    text
    copied!<p>I'm working on a project that will feature many of these 'second by second' timers that will display how many animals are born per second :) Do you have any suggestions as to how I could best optimize this script for performance and DRY-ish?</p> <p>JSFiddle here - <a href="http://jsfiddle.net/gJy4x/32/" rel="nofollow">http://jsfiddle.net/gJy4x/32/</a> </p> <pre><code>var start = new Date(), midnight = new Date(start.getFullYear(), start.getMonth(), start.getDate(), 0), first = new Date(start.getFullYear(), start.getMonth(), 1); setInterval(function () { var now = new Date(), secondsFromStart = Math.floor((now - start)/1000), secondsFromMidnight = Math.floor((now - midnight)/1000), secondsFromFirst = Math.floor((now - first)/1000), puppiesBornPerSec = 3.16, puppiesStart = Math.round(secondsFromStart*puppiesBornPerSec*100) / 100, puppiesMidnight = Math.round(secondsFromMidnight*puppiesBornPerSec*100) / 100, puppiesFirst = Math.round(secondsFromFirst*puppiesBornPerSec*100) / 100; // 3 puppies born every second $('#badge01').text(puppiesStart.toFixed(2)); $('#s01 .morning').text(puppiesMidnight.toFixed(2)); $('#s01 .month').text(puppiesFirst.toFixed(2)); // 5 kittens born every second $('#badge02').text(Math.floor(secondsFromStart*5)); $('#s02 .morning').text(Math.floor(secondsFromMidnight*5)); $('#s02 .month').text(Math.floor(secondsFromFirst*5)); // 7 rats born every second $('#badge03').text(Math.floor(secondsFromStart*7)); $('#s03 .morning').text(Math.floor(secondsFromMidnight*7)); $('#s03 .month').text(Math.floor(secondsFromFirst*7)); }, 1000); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload