【发布时间】:2015-11-07 15:26:01
【问题描述】:
- tem.jqw.Callback 对象包含一个可执行函数,一个 CSS 选择器。和循环延迟。
- tem.jqw.wait 等待 jQuery 被加载,然后遍历 tem.jqw.Callback 对象数组并在找到具有 CSS 的元素后执行它们的函数选择器传入。
我遇到的问题是 tem.jqw.Callback 对象中的 run 函数。第一次调用 run 函数时,如果元素存在,则一切正常,可执行函数运行正常。但是,如果元素还不存在,并且我们需要循环,则函数在 setTimeout(this.run, 100) 执行一次后失去作用域。例如,当 run 函数第二次执行时,this.selector 或 this.fn 变为未定义。在不使用全局变量的情况下如何解决这个问题?提前致谢。
if (typeof tem !== "object") {
tem = {};
}
tem.jqw = {};
tem.jqw.retries = 0;
tem.jqw.maxRetries = 100;
tem.jqw.delay = 100;
tem.jqw.callbacks = [];
tem.jqw.Callback = function (fn, selector, delay) {
this.fn = fn;
this.selector = (typeof selector === "string" && selector.length > 0) ? selector : document;
this.delay = (typeof delay === "number" && delay > 0 && delay < 1000) ? delay : 100;
this.retries = 0;
this.maxRetries = 100;
this.start = function () {
this.run();
};
this.run = function () {
if (jQuery(this.selector).length > 0) {
console.log("[OPDEBUG] tem.jqw.Callback.run says: " + this.selector.toString() + " is ready. Executing callback function...");
this.fn();
} else {
this.retries++;
console.log("[OPDEBUG] tem.jqw.Callback.run says: typeof this.selector " + typeof this.selector);
console.log("[OPDEBUG] tem.jqw.Callback.run says: Waiting for " + this.selector.toString() + "...");
if (this.retries < this.maxRetries) {
setTimeout(this.run, 100);
}
}
};
};
tem.jqw.wait = function () {
if (typeof jQuery === "function") {
console.log("[OPDEBUG] tem.jqw.wait says: jQuery is ready.");
for (var i = 0; i < tem.jqw.callbacks.length; i++) {
if (typeof tem.jqw.callbacks[i] === "object" && typeof tem.jqw.callbacks[i].start === "function") {
console.log("[OPDEBUG] tem.jqw.wait says: Executing callback function " + (i + 1) + "...");
tem.jqw.callbacks[i].start();
}
}
} else {
tem.jqw.retries++;
console.log("[OPDEBUG] tem.jqw.wait says: " + "Waiting for jQuery " + tem.jqw.retries + "...");
if (tem.jqw.retries < tem.jqw.maxRetries) {
setTimeout(tem.jqw.wait, tem.jqw.delay);
}
}
};
tem.jqw.callbacks.push(new tem.jqw.Callback(function () {
jQuery('.hero-inner:first a').css('background-image', 'url("https://www.thedogs.co.nz/Files/PhotoFinishImages/11298_89160.jpg")')
}, ".hero-inner:first"));
tem.jqw.callbacks.push(new tem.jqw.Callback(function () {
jQuery('.RR-placeholder ul li:first').hide();
}, ".RR-placeholder ul li:first"));
tem.jqw.wait();
【问题讨论】:
-
setTimeout(this.run.bind( this ), 100)
标签: javascript object recursion scope closures