I have a script for a countdown function, which is using massive amounts of CPU power on my MBP.
The script updates the countdown display every second and is updating all piecharts as well.
var options = {
scaleColor: false,
trackColor: 'rgba(255,255,255,0.3)',
barColor: '#E7F7F5',
lineWidth: 6,
lineCap: 'butt',
size: 95
};
$('#days').easyPieChart(options);
$('#hours').easyPieChart(options);
$('#minutes').easyPieChart(options);
$('#seconds').easyPieChart(options);
function countdown(endT,callback) {
var first_load = false;
var days,hours,minutes,sec,timer;
end = new Date(endT);
end = end.getTime(); //Get initial Date in Milliseconds
if (isNaN(end)) {
return;
}
var tot_current = new Date();
var tot_remain = parseInt((end - tot_current.getTime())/1000);
var tot_days = parseInt(tot_remain/86400);
timer = setInterval(calculate,1000);
function calculate(){
var current = new Date();
var remaining = parseInt((end - current.getTime())/1000); //remaining seconds
if (remaining <= 0){
clearInterval(timer);
days=0;
hours=0;
minutes=0;
sec=0;
display(days,hours,minutes,sec);
if (typeof callback === 'function' ) {
callback();
}
}else{
days = parseInt(remaining/86400);
remaining = (remaining%86400);
hours = parseInt(remaining/3600);
remaining = (remaining%3600);
minutes = parseInt(remaining/60);
remaining = (remaining%60);
sec = parseInt(remaining);
display(days,hours,minutes,sec);
}
}
function display(days,hours,minutes,sec) {
var dl = days.toString().length;
if (dl == "1") {
sl = 2;
}else{
if (isNaN(dl)) {
sl = 3;
}
sl = dl;
}
days_rem = ("00"+days).slice(-sl);
hrs_rem = ("0"+hours).slice(-2);
min_rem = ("0"+minutes).slice(-2);
sec_rem = ("0"+sec).slice(-2);
$("#days span").text(days_rem);
$("#hours span").text(hrs_rem);
$("#minutes span").text(min_rem);
$("#seconds span").text(sec_rem);
$("#days").data('easyPieChart').update((100/tot_days)*days_rem);
// Disable animation for the first load
if(hrs_rem == 23 && first_load) { $('#hours').data('easyPieChart').disableAnimation(); }
$("#hours").data('easyPieChart').update((100/23)*hrs_rem);
if(hrs_rem == 23 && first_load) { $('#hours').data('easyPieChart').disableAnimation(); }
if(min_rem == 59 && first_load) { $('#minutes').data('easyPieChart').disableAnimation(); }
$("#minutes").data('easyPieChart').update((100/59)*min_rem);
if(min_rem == 59 && first_load) { $('#minutes').data('easyPieChart').enableAnimation(); }
if(sec_rem == 59 && first_load) { $('#seconds').data('easyPieChart').disableAnimation(); }
$("#seconds").data('easyPieChart').update((100/59)*sec_rem);
if(sec_rem == 59 && first_load) { $('#seconds').data('easyPieChart').enableAnimation(); }
first_load = true;
}
}
var d = new Date(2015, 6, 10, 12, 7, 0, 0);
countdown(d,null);
The code can be seen in action here.