改变NaN和未定义

时间:2014-03-16 15:14:40

标签: javascript jquery

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

这是一个截图,显示我在说什么。 http://i.stack.imgur.com/hrgKd.png

谢谢。 乔

2 个答案:

答案 0 :(得分:0)

在你的代码中你有这个功能:

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,如果是,则显示已完成的消息。我不确定为什么uploadedBytestotalBytes正在返回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');
    }
}

答案 1 :(得分:0)

检查此声明:

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

您错过了为humanReadableTime指定(uploadedBytes / bytesPerSec)。所以它应该是:

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