Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Add commas to Values being updated with setInterval()
    primarykey
    data
    text
    <p>I have a script that calculates how many (puppies and kittens) are being per second. It uses Date() to calculate how many have been born sine the beginning of the week an the beginning of the month. But I'm having difficulty updating this script to add commas at the thousands, millions, billions mark. Can someone please show me the best way to add commas in this script?</p> <p>here goes the <a href="http://jsfiddle.net/gJy4x/35" rel="nofollow">jsFiddle</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); var now = new Date(), secondsFromStart = Math.floor((now - start)/1000), secondsFromMidnight = Math.floor((now - midnight)/1000), secondsFromFirst = Math.floor((now - first)/1000); var elems = []; $(".s").each(function(){ var $this = $(this); var BornPerSec = $this.data("quantity"), Start = secondsFromStart*BornPerSec, Midnight = secondsFromMidnight*BornPerSec, First = secondsFromFirst*BornPerSec; elems.push({ obj: $this, BornPerSec: BornPerSec, Start : Start, Midnight : Midnight, First : First, now: $this.children('.now'), morning: $this.children('.morning'), month: $this.children('.month'), }); }); setInterval(function () { $.each(elems,function(i,n){ n.Start+=n.BornPerSec; n.Midnight+=n.BornPerSec; n.First+=n.BornPerSec; n.now.text(n.Start % 1 === 0 ?n.Start: n.Start.toFixed(2)); n.morning.text(n.Midnight % 1 === 0 ?n.Midnight:n.Midnight.toFixed(2)); n.month.text(n.First % 1 === 0 ?n.First:n.First.toFixed(2)); }); }, 1000); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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