私は、0:03:15のような時間文字列を含む動的に生成されたdivの負荷があります。これらは歌の継続時間です。 私はこれらの時間を一緒に加え、時間の合計を表示するスクリプトを持っていますが、私は時間、分、秒しか働くことができません。私は日、時間、分、秒を得ようとしています。ここでdivsでの時間の合計どのように日を表示するのですか?
は、私が試してみました初心者のものの一部と私のコードです:
function getSeconds(time) {
var parts = time.split(":");
return parseInt(parts[0], 12) * 86400 + parseInt(parts[1], 10) * 3600 + parseInt(parts[2], 10) * 60 + parseInt(parts[3], 10);
// return parseInt(parts[0], 10) * 3600 + parseInt(parts[1], 10) * 60 + parseInt(parts[2], 10);
}
//select all the elements
var totalSeconds = $(".durationhold")
.map(function(ind, elem) { //convert the jQuery object into the array
var text = $(elem).text(); //get the text from the anchor
return getSeconds(text); //set the index to the total seconds
})
.get() //gets the array out of the jQuery object
.reduce(function(runningTotal, currentValue){ //Now to combine all the values into one
return runningTotal + currentValue; //sum up the values
},0); //The initial starting value
//Now get the hour, minutes, and seconds from the total seconds
var days = parseInt (totalSeconds/86400);
var hours = parseInt(totalSeconds/3600);
var minutes = parseInt(totalSeconds/60) % 60;
var seconds = totalSeconds % 60;
//left pad numbers less than ten
if(hours<10) hours = "0" + hours;
if(minutes<10) minutes = "0" + minutes;
if(seconds<10) seconds = "0" + seconds;
$("#out").html("Total Time: " + (days + ":" + hours + ":" + minutes + ":" + seconds));
は、ここにデモのためのいくつかの作ら倍とfiddleです。 同じことをより速く実行する方法はありますか? 1つから5000以上の結果が得られます。 ありがとうございました。
ありがとうPevara、図書館はうまくいきます。一度1000を超えると結果が表示されるのに10-15秒ほど遅くなります。すぐに表示するにはあまりにも多くのデータがあると思います。最初のメソッドは、約600曲を打ち切って、各数値に対してNaNを返します。 –
私はおそらく実際の(ミリ秒)秒の値をあなたのhtml上の属性として表示することに目を向けるでしょう。そうすれば、これらの値を合計し、その値で継続時間を開始することができます。そうすれば、多くの構文解析をスキップすることができます。そのため、かなり良い結果が得られます。 – Pevara
私はmoments.jsであなたのコードを使い終わり、他のスクリプトを動かして3000曲の約1秒にしました。Woo Hoo。どうもありがとうございました。 –