JQuery / JavaScript Schrittzahl
-
05-07-2019 - |
Frage
Ich versuche, jede Sekunde eine Zahl um einen bestimmten Wert zu erhöhen und die Formatierung mit JavaScript oder JQuery beibehalten
Ich kämpfe, es zu tun.
Sagen wir, ich habe eine Reihe wie folgt:
1412015
die Nummer, die dies kann durch jede Sekunde erhöht werden variabel ist es etwas beween 0,1 und 2 sein könnte.
Ist es möglich, wenn der Wert, der durch jede Sekunde 0,54 erhöht werden muss, um die Zahl zu incremenet und haben die folgende Ausgabe:
1.412.016
1.412.017
1.412.018
Danke
Eef
Lösung
Ich bin nicht ganz sicher, ob ich Ihre Inkrementierung Fall verstehen und was Sie wollen zeigen. Aber ich entschied sich für eine Lösung zu läuten in eine Reihe zu formatieren.
Ich habe zwei Versionen ein Zahlenformat Routine bekommt, eine, die ein Array analysiert, und eine, die mit einem regulären Ausdruck formatiert. Ich werde zugeben, dass sie nicht die am einfachsten zu lesen sind, aber ich hatte Spaß mit dem Ansatz kommen.
Ich habe versucht, die Linien mit Kommentaren in Fall zu beschreiben, du bist neugierig
Array-Parsing-Version:
function formatNum(num) {
//Convert a formatted number to a normal number and split off any
//decimal places if they exist
var parts = String( num ).replace(/[^\d.]-/g,'').split('.');
//turn the string into a character array and reverse
var arr = parts[0].split('').reverse();
//initialize the return value
var str = '';
//As long as the array still has data to process (arr.length is
//anything but 0)
//Use a for loop so that it keeps count of the characters for me
for( var i = 0; arr.length; i++ ) {
//every 4th character that isn't a minus sign add a comma before
//we add the character
if( i && i%3 == 0 && arr[0] != '-' ) {
str = ',' + str ;
}
//add the character to the result
str = arr.shift() + str ;
}
//return the final result appending the previously split decimal place
//if necessary
return str + ( parts[1] ? '.'+parts[1] : '' );
}
Regular Expression Version:
function formatNum(num) {
//Turn a formatted number into a normal number and separate the
//decimal places
var parts = String( num ).replace(/[^\d.]-/g,'').split('.');
//reverse the string
var str = parts[0].split('').reverse().join('');
//initialize the return value
var retVal = '';
//This gets complicated. As long as the previous result of the regular
//expression replace is NOT the same as the current replacement,
//keep replacing and adding commas.
while( retVal != (str = str.replace(/(\d{3})(\d{1,3})/,'$1,$2')) ) {
retVal = str;
}
//If there were decimal points return them back with the reversed string
if( parts[1] ) {
return retVal.split('').reverse().join('') + '.' + parts[1];
}
//return the reversed string
return retVal.split('').reverse().join('');
}
Angenommen, Sie ausgeben möchten eine formatierte Zahl in jeder Sekunde um 0,54 erhöht ein Intervall verwenden könnte Ihre Inkrementierung und Ausgabe zu tun.
Super Short Firefox mit Firebug nur Beispiel:
var num = 1412015;
setInterval(function(){
//Your 0.54 value... why? I don't know... but I'll run with it.
num += 0.54;
console.log( formatNum( num ) );
},1000);
Sie können alles in Aktion sehen hier: http://jsbin.com/opoze
Andere Tipps
Um diese Struktur einen Wert an jedem zweiten Einsatz zu erhöhen:
var number = 0; // put your initial value here
function incrementNumber () {
number += 1; // you can increment by anything you like here
}
// this will run incrementNumber() every second (interval is in ms)
setInterval(incrementNumber, 1000);
Dies formatiert Zahlen für Sie:
function formatNumber(num) {
num = String(num);
if (num.length <= 3) {
return num;
} else {
var last3nums = num.substring(num.length - 3, num.length);
var remindingPart = num.substring(0, num.length - 3);
return formatNumber(remindingPart) + ',' + last3nums;
}
}
function rounded_inc(x, n) {
return x + Math.ceil(n);
}
var x = 1412015;
x = rounded_inc(x, 0.54);