多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 事件循环 ```html <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Language" content="zh-cn"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"> <meta name="keywords" content="keywords"> <meta name="description" content="description"> <link href="favicon.ico" rel="icon" type="image/x-icon"> <script type="text/javascript" src="web/res/js/jquery.1.9.1.min.js"></script> <title>Document</title> </head> <body> <script> function test(opt) { me = this; for (var _ in opt) { this[_] = opt[_]; } this.i = 0; this._uparr = [{ name: 'a', up: 0 }, { name: 'b', up: 0 }, ]; this.f = null; this.start = function() { this.up(0); }; this.up = function(o) { if (o >= this._uparr.length) { return; } if (!this._uparr[o] || this._uparr[o].up == 1) { this.up(o + 1); return; } this.i = o; this.f = this._uparr[o]; // this.upOk = function () { // }; this.upHandle(this.f); }; this.upOk = function(f) { console.log('upOk ', f, this.f, this.i); }; this.nextUp = function () { this.up(this.i + 1); }; this.ossTSTUploadBack = function (f) { console.log('ossTSTUploadBack start ... ', f, this.f, this.i); setTimeout(function() { me.upOk(me.f); me.upOk_(me.f); // 不必等到上传成功后才上传下一个文件,可以并发上传 me.nextUp(); }, 20); }; this.upHandle = function(f) { console.log('upHandle start ... ', f, this.f, this.i); setTimeout(function() { me.ossTSTUploadBack(me.f); }, 20); }; this.oo = function () { for (_ in this._uparr) { this.i = _; this.f = this._uparr[_]; this.t(this.f); } }; this.t = function (f) { console.log('t ... ', f, this.f, this.i); var ff = this.f; var ff_str = JSON.stringify(ff); // setTimeout 不能记住 task 定义时 的 上下文,在 放到 Stack 执行 时,此时 task 有最新的上下文 // 除非,定义 task 时 用一个单独的 变量记住此时的值,这样 等到 task 工作时,就能使用之前定义时的上下文了 // setTimeout 事件循环 上下文特性 和 ajax 是一样的 setTimeout(function() { console.log('t t ... ', f, me.f, me.i, ff, ff_str); }, 20); }; } var opt = { upOk_: function (f) { console.log('upOk_ ', f, t.f, t.i); } }; var t = new test(opt); </script> </body> </html> ```