admin管理员组文章数量:1290263
I am sourcing a large mapping/widget javascript file (1.3 MB) and wanted to display a progress bar as it loads. I know firebug's net watch tab knows a lot of this information, but I would like something more lightweight. I came across this website: /
which almost gets me there except that I need to source the file I'm downloading. I didn't see any listeners on jQuery's getScript as the file downloads. Does anyone know how to get at the progress of a sourced file download?
Thanks in advance!
I am sourcing a large mapping/widget javascript file (1.3 MB) and wanted to display a progress bar as it loads. I know firebug's net watch tab knows a lot of this information, but I would like something more lightweight. I came across this website: http://blog.greweb.fr/2012/04/work-in-progress/
which almost gets me there except that I need to source the file I'm downloading. I didn't see any listeners on jQuery's getScript as the file downloads. Does anyone know how to get at the progress of a sourced file download?
Thanks in advance!
Share Improve this question asked Jun 29, 2012 at 16:57 JonathanJonathan 9142 gold badges9 silver badges22 bronze badges1 Answer
Reset to default 8If you use getScript()
you can execute a function using the success callback but that is only when the script has finished loading.
I would remend having a loading indicator image (you can find many at http://ajaxload.info/) and hiding it when the script has loaded.
This SO has a couple of other ideas. One solution is below:
var myTrigger;
var progressElem = $('#progressCounter');
$.ajax ({
type : 'GET',
dataType : 'xml',
url : 'somexmlscript.php' ,
beforeSend : function (thisXHR)
{
myTrigger = setInterval (function ()
{
if (thisXHR.readyState > 2)
{
var totalBytes = thisXHR.getResponseHeader('Content-length');
var dlBytes = thisXHR.responseText.length;
(totalBytes > 0)? progressElem.html (Math.round ((dlBytes/ totalBytes) * 100) + "%") : progressElem.html (Math.round (dlBytes /1024) + "K");
}
}, 200);
},
plete : function ()
{
clearInterval (myTrigger);
},
success : function (response)
{
// Process XML
}
});
This sets an interval to pute the progress by taking loaded bytes and total bytes. This might work for you.
本文标签: jqueryJavascript source file download progressStack Overflow
版权声明:本文标题:jquery - Javascript source file download progress? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741493728a2381737.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论