Javascript 更改NaN和未定义

Javascript 更改NaN和未定义,javascript,jquery,Javascript,Jquery,上传完成后,在我的上传页面上,没有显示预期的数字,而是显示undefined和NaN,我想知道是否有任何方法可以将undefined更改为显示文件大小,将NaN更改为显示0 这里有一个截图来显示我在说什么。 多谢各位。 Joe在您的代码中,您具有以下功能: function updateProgessText(progress, uploadedBytes, totalBytes) { nowTime = (new Date()).getTime(); loadTime = (nowTime

上传完成后,在我的上传页面上,没有显示预期的数字,而是显示undefined和NaN,我想知道是否有任何方法可以将undefined更改为显示文件大小,将NaN更改为显示0

这里有一个截图来显示我在说什么。

多谢各位。
Joe

在您的代码中,您具有以下功能:

function updateProgessText(progress, uploadedBytes, totalBytes) {
nowTime = (new Date()).getTime();
loadTime = (nowTime - startTime);
if (loadTime == 0) {
loadTime = 1;
}
loadTimeInSec = loadTime / 1000;
bytesPerSec = uploadedBytes / loadTimeInSec;
textContent = '';
textContent += '' + progress + '% complete';
textContent += ' ';
textContent += '(' + bytesToSize(uploadedBytes, 2) + ' of ' + bytesToSize(totalBytes, 2) + ')';
$("#fileupload-progresstextLeft").html(textContent);
rightTextContent = '';
rightTextContent += '' + humanReadableTime((totalBytes / bytesPerSec) - (uploadedBytes / bytesPerSec)) + ' remaining';
rightTextContent += ' at ' + bytesToSize(bytesPerSec, 2) + 'P/s';
$("#fileupload-progresstextRight").html(rightTextContent);
}
您需要检查进度是否为100,如果是,则显示已完成的消息。我不确定为什么
uploadedBytes
totalBytes
返回
undefined
,我猜这与JQuery文件上传插件有关。您可以这样做:

function updateProgessText(progress, uploadedBytes, totalBytes) {
    if (progress < 100) {
        nowTime = (new Date()).getTime();
        loadTime = (nowTime - startTime);
        if (loadTime == 0) {
        loadTime = 1;
        }
        loadTimeInSec = loadTime / 1000;
        bytesPerSec = uploadedBytes / loadTimeInSec;
        textContent = '';
        textContent += '' + progress + '% complete';
        textContent += ' ';
        textContent += '(' + bytesToSize(uploadedBytes, 2) + ' of ' + bytesToSize(totalBytes, 2) + ')';
        $("#fileupload-progresstextLeft").html(textContent);
        rightTextContent = '';
        rightTextContent += '' + humanReadableTime((totalBytes / bytesPerSec) - (uploadedBytes / bytesPerSec)) + ' remaining';
        rightTextContent += ' at ' + bytesToSize(bytesPerSec, 2) + 'P/s';
        $("#fileupload-progresstextRight").html(rightTextContent);
    }
    else {
        $("#fileupload-progresstextLeft").html('100% complete');
        $("#fileupload-progresstextRight").html('0 seconds remaining');
    }
}
函数updateProgesText(进度、上载字节、总字节){
如果(进度<100){
nowTime=(新日期()).getTime();
加载时间=(nowTime-startTime);
如果(加载时间==0){
加载时间=1;
}
loadTimeInSec=loadTime/1000;
bytesPerSec=上传字节/loadTimeInSec;
textContent='';
textContent+=''+进度+'%complete';
textContent+='';
textContent+='('+bytesToSize(uploadedBytes,2)+'of'+bytesToSize(totalBytes,2)+');
$(“#文件上传进度TextLeft”).html(文本内容);
rightTextContent='';
rightTextContent+=''+humanReadableTime((totalBytes/bytesPerSec)-(uploadedBytes/bytesPerSec))+“剩余”;
rightTextContent+='at'+bytesToSize(bytesPerSec,2)+'P/s';
$(“#fileupload progresstextRight”).html(rightextcontent);
}
否则{
$(“#文件上传进度文本左”).html('100%完成');
$(“#fileupload progresstextRight”).html(剩余0秒);
}
}
检查以下语句:

rightTextContent += '' + humanReadableTime((totalBytes / bytesPerSec) - (uploadedBytes / bytesPerSec)) + ' remaining';
您没有为
(uploadedBytes/bytesPerSec)
指定
humanReadableTime
。因此,它应该是:

rightTextContent += '' + humanReadableTime((totalBytes / bytesPerSec) - humanReadableTime(uploadedBytes / bytesPerSec)) + ' remaining';

请在此处包含您的代码。您使用的上载库是什么?这些细节对于提供帮助至关重要。这是一个很大的脚本,因此很遗憾无法在此处复制,但如果您可以在chillingsafe.com/upload.php上查看源代码并查找从第46行开始的脚本,将不胜感激。谢谢。你如何在js代码中获取大小?因此,我们可以提供尽可能接近您预期的线索。感谢您的回复,但现在一切都很好:)谢谢您,但我无法让这项工作。抱歉,但没有您的代码片段,我们真的无法提供帮助。查看第46行的源代码是脚本chillingsafe.com/upload.php检查我对答案所做的编辑。我不知道为什么uploadedBytes和totalBytes会返回未定义。我猜这与jQuery插件有关。非常感谢您,现在一切都很完美,当进度达到100%时,添加了此代码(未定义的B中的未定义的B)就会消失,在我看来,这比在那里有信息要好得多。再次感谢你。