观看房产创建活动? [英] Watch for a property creation event?

查看:73
本文介绍了观看房产创建活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够确定何时创建对象(不是DOM元素 - 一个javascript对象)。

I need to be able to determine when an object is created (not a DOM element -- a javascript object).

对这个问题的回答有一些用于创建可观察属性的非常有用的代码,因此,当属性发生变化时,您可以触发函数。

An answer to this question has some very useful looking code for creating observable properties, so you can have a function fire when a property changes.

在我的情况下,我需要在创建对象/属性时执行某些操作,而不是更改现有属性,并且我对这些问题的理解有限,并没有帮助我弄清楚是否或如何使用该代码在经过多次眯眼之后这样做。

In my situation I need to do something when the object/property is created, not an existing property changed, and my limited understanding of such matters did not help me figure out if or how I could use that code to do this after much squinting.

情况是:页面加载一堆脚本一些脚本创建了其他脚本所需的东西,例如:

The situation is: page loads a bunch of scripts. Some of the scripts create things that are needed by other scripts, e.g:

ThisStuff = (function () {
  // blah blah
   return self;
} ());

其他一些代码需要初始化 ThisStuff ,只要它可用,这可能是在DOM完成加载之后。用户实际上并不需要 ThisStuff ,所以只要脚本加载完成就可以了。所以我想按照以下几点做一些事情:

Some other code needs to initialize this ThisStuff, whenever it's available, which may be after the DOM is done loading. The user doesn't actually need ThisStuff right away, so it's fine for it to happen whenever the script is done loading. So I would like to do something along lines of:

$(document).ready(function() {
   wheneverIsAvailable(window,'ThisStuff', function(object) {
       object.init(args);
   })
});

我意识到此问题还有其他解决方案(更改脚本顺序或按需加载脚本)但那些因为建筑而很难。所以我只对与其他解决方案相比的方法感兴趣。如果jQuery提供了一些这样的功能,那么我也可以使用它。

I realize there are other solutions to this problem (changing script order, or loading scripts on demand) but those are difficult because of the architecture. So I'm only interested in a way to do this versus other solutions. If jQuery offers some such functionality, that's fine also as I'm using it.

推荐答案

你可以拥有 setInterval 每秒检查一次以查看特定变量。您可以使用 obj.hasOwnProperty(prop)检查它是否是创建的。创建它时,你调用该函数,并清除间隔。

You could have a setInterval checking a number of times a second to watch the specific variable. You can check whether it is created using obj.hasOwnProperty(prop). When it is created, you invoke the function, and clear the interval.

它可能很脏,但它也可能适合你。

It might be dirty but it might also just work fine for you.

编辑:我为您编写了这个代码: http: //jsfiddle.net/jhXJ2/2/ 。它还支持向函数传递其他参数。

I coded this for you: http://jsfiddle.net/jhXJ2/2/. It also supports passing additional arguments to the function.

window.__intervals = [];

function wheneverIsAvailable(obj, prop, func) {
    var id = (Math.random()+"").substring(2);
    var args = arguments;
    window.__intervals[id] = window.setInterval(function() {
        if(obj.hasOwnProperty(prop)) {
            window.clearInterval(window.__intervals[id]);
            func(Array.prototype.slice.call(args, 3));
            // Call function with additional parameters passed
            // after func (from index 3 and on)
        }
    }, 1000/ 50);
}

wheneverIsAvailable(window, 'test', function() {
    alert(arguments[0]);
}, 'Woot!');

window.setTimeout('window.test = 123', 1000);

这篇关于观看房产创建活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆