var nowsize="";记录上次下载数据包大小
var st=new Date()记录上次刷新进度条时间
1 case 3: 2 var et=new Date() 3 var current = parseInt(100 * task.downloadedSize / task.totalSize); 4 // plus.notification.setProgress(current); //插件调用 5 document.getElementById("jdt").style.display=""; 6 document.getElementById("jdt").style.width=current+'%'; 7 if(((et-st)/1000)>1){ 8 document.getElementById("jdt").innerText=current+'%'+"|"+changeSize((1000*(task.downloadedSize-nowsize))/(et-st))+"/s"; 9 nowsize=task.downloadedSize; 10 st=new Date(); 11 } 12 break;
网络速度转换
1 function changeSize(size) { 2 var num; 3 if((size / 1024) <= 1024) { 4 num = size / 1024; 5 return num.toFixed(2) + 'KB'; 6 } 7 else if((size / 1024) > 1024 && (size / 1024) <= (1024 * 1024)) { 8 num = (size / 1024) / 1024; 9 return num.toFixed(2) + 'M'; 10 } 11 else if((size / 1024) > (1024 * 1024)) { 12 num = ((size / 1024) / 1024) / 1024 13 return num.toFixed(2) + 'G'; 14 } 15 else{ 16 return size + 'B'; 17 } 18 }