Chr*_*ris 5 jquery html5 xmlhttprequest
我通过XHR上传文件时订阅了onProgress事件.我的进度条是动画的(通过jQuery),以提供更好的视觉美感.
onProgress似乎非常快速,所以我想知道它实际上被解雇的频率,以便我可以以某种方式设计一个过程,我可以限制对此的响应,以便我可以有一个连续的动画进度条
虽然扩展jQuery可能是有益的; 对于这个简单的扩展jQuery不值得花费的东西.限制函数调用的有效解决方案可写为:
xhr.upload.onprogress = function(event) {
// limit calls to this function
if (!this.NextSecond) this.NextSecond = 0;
if (Date.getTime() < this.NextSecond) return;
this.NextSecond = Date.getTime() + 1000;
// this code gets executed at least one second apart from the last time
}
Run Code Online (Sandbox Code Playgroud)
查看jQuerythrottle/debounce 插件来限制对回调的调用onprogress。
节流演示:http://benalman.com/code/projects/jquery-throttle-debounce/examples/throttle/
你的代码看起来像这样:
xhr.upload.onprogress = $.throttle(100, function (event)
{
// update the progress bar
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3127 次 |
| 最近记录: |