Javascript 在变量更新后执行函数
我正在编写模块化JavaScript,我有一个特定的函数,可以完成整个lotta处理,即。绘制2张画布,更新大量变量并存储对象引用。现在我想执行另一个函数,它使用上面更新的一些变量 大概是这样的:Javascript 在变量更新后执行函数,javascript,Javascript,我正在编写模块化JavaScript,我有一个特定的函数,可以完成整个lotta处理,即。绘制2张画布,更新大量变量并存储对象引用。现在我想执行另一个函数,它使用上面更新的一些变量 大概是这样的: 绘制画布-将图像尺寸存储在变量中(以及许多其他内容) 使用这些尺寸做一些数学和几何,再次更新画布!我不能在第一个函数中做这个计算,因为它是我用来绘制画布的常用实用函数,在我的代码中无处不在 如果我在代码中插入setTimeout10秒,一切正常,但是如果没有它,上面的第二条指令找不到更新的变量,因
- 绘制画布-将图像尺寸存储在变量中(以及许多其他内容)
- 使用这些尺寸做一些数学和几何,再次更新画布!我不能在第一个函数中做这个计算,因为它是我用来绘制画布的常用实用函数,在我的代码中无处不在
setTimeout
10秒,一切正常,但是如果没有它,上面的第二条指令找不到更新的变量,因此失败
有办法解决这个问题吗?也就是说,我只想在设置了一些必需的变量之后执行第二条指令。我说,同步执行
注意:我不能在这里(或任何地方)发布任何代码,因为这在我的工作场所是不允许的 对于这种情况,我建议使用。只需在第一个函数完成画布更新后发布事件。第二个函数(以及其他任何函数)可以监听这些事件并执行它们想要的任何操作 赞成者:
- 无耦合
- 单个部件易于测试
- 可扩展
- 需要jQuery,否则需要提取事件处理代码
- 无耦合
- 单个部件易于测试
- 可扩展
- 需要jQuery,否则需要提取事件处理代码
- 对于这种情况,我建议使用。只需在第一个函数完成画布更新后发布事件。第二个函数(以及其他任何函数)可以监听这些事件并执行它们想要的任何操作
赞成者:
在setter中,您可以进行一些计算,检查是否满足某些条件 并在需要时更新 给你一个想法:
// updateFunc is the function called whenever a property changes
// and all conditions are met for an update.
// newProp1,2,3 are the new values for prop1,2,3
function MyStorageClass(updateFunc, newProp1, newProp2, newProp3 ) {
this.updateFunc = updateFunc;
this.prop1 = newProp1 ;
this.prop2 = newProp2 ;
this.prop3 = newProp3 ;
}
var MSCProto = MyStorageClass.prototype;
// update is needed if all properties are >0
MSCProto.checkUpdateRequired = function() {
return ( ( this.prop1 > 0 ) && (this.prop2 > 0) && (this.prop3 > 0) )
}
Object.defineProperty(MSCProto, 'prop1', {
get : function() { retrurn this._prop1},
set : function(x) { this._prop1 = x;
// and some other computations if need be
if (this.checkUpdateRequired()) this.updateFunc(); } };
Object.defineProperty(MSCProto, 'prop2', {
get : function() { retrurn this._prop2},
set : function(x) { this._prop2 = x;
// and some other computations if need be
if (this.checkUpdateRequired()) this.updateFunc(); } };
Object.defineProperty(MSCProto, 'prop3', {
get : function() { retrurn this._prop3},
set : function(x) { this._prop3 = x;
// and some other computations if need be
if (this.checkUpdateRequired()) this.updateFunc(); } };
您可以使用getter和setter来监视给定条件。
在setter中,您可以进行一些计算,检查是否满足某些条件 并在需要时更新 给你一个想法:
// updateFunc is the function called whenever a property changes
// and all conditions are met for an update.
// newProp1,2,3 are the new values for prop1,2,3
function MyStorageClass(updateFunc, newProp1, newProp2, newProp3 ) {
this.updateFunc = updateFunc;
this.prop1 = newProp1 ;
this.prop2 = newProp2 ;
this.prop3 = newProp3 ;
}
var MSCProto = MyStorageClass.prototype;
// update is needed if all properties are >0
MSCProto.checkUpdateRequired = function() {
return ( ( this.prop1 > 0 ) && (this.prop2 > 0) && (this.prop3 > 0) )
}
Object.defineProperty(MSCProto, 'prop1', {
get : function() { retrurn this._prop1},
set : function(x) { this._prop1 = x;
// and some other computations if need be
if (this.checkUpdateRequired()) this.updateFunc(); } };
Object.defineProperty(MSCProto, 'prop2', {
get : function() { retrurn this._prop2},
set : function(x) { this._prop2 = x;
// and some other computations if need be
if (this.checkUpdateRequired()) this.updateFunc(); } };
Object.defineProperty(MSCProto, 'prop3', {
get : function() { retrurn this._prop3},
set : function(x) { this._prop3 = x;
// and some other computations if need be
if (this.checkUpdateRequired()) this.updateFunc(); } };
如何在一句话中使用“模块化JavaScript”和“大量全局语言”你抓住我了我指的是一些存储为属性的值。更新描述…您可以使用
window.postMessage
和window.addEventListener(“消息”…
通知您的程序执行特定任务。如何在一句话中使用“模块化JavaScript”和“大量全局文件?”:-)您抓到我了!;)我指的是一些存储为属性的值。更新描述…您可以使用window.postMessage
和window.addEventListener(“消息”…
通知您的程序执行特定任务。您的意思是完成时触发?(我使用jQuery完成80%的工作!)是的。这样,任何数量的独立侦听器都可以执行某些操作。您的意思是完成时触发?(我80%的工作都在使用jQuery!)是的。这样,任何数量的独立侦听器都可以做一些事情。