Javascript 我可以做一个变量';s值取决于对另一个变量的更改?
我有一个javascript变量Javascript 我可以做一个变量';s值取决于对另一个变量的更改?,javascript,lodash,Javascript,Lodash,我有一个javascript变量a和一个变量b,其值可以是0或1 有谁能建议我如何对函数进行编码,使b取决于a的值,如下所示: 当a从0变为1时-如果a大于500ms时为1,则b设置为1 当a从1变为0时-b立即设置为0 如果有一种方法可以使用函数对此进行编码,那么该函数是否可以附加到变量a的setter?我就是这样做的,只需定义一个可以访问外部范围的toggleA函数: var a = 0, b, t, toggleA = function() { switch(a) { ca
a
和一个变量b
,其值可以是0或1
有谁能建议我如何对函数进行编码,使b
取决于a
的值,如下所示:
- 当
从0变为1时-如果a
大于500ms时为1,则a
设置为1b
- 当
从1变为0时-a
立即设置为0b
如果有一种方法可以使用函数对此进行编码,那么该函数是否可以附加到变量
a的
setter?我就是这样做的,只需定义一个可以访问外部范围的toggleA
函数:
var a = 0, b, t,
toggleA = function() {
switch(a) {
case 0:
a = 1;
t = window.setTimeout(function(){ b = 1; }, 500);
break;
case 1:
window.clearTimeout(t);
a = b = 0;
break;
}
};
调用
toggleA()
将a
s值切换到1和0之间。根据将a
的值从1切换到0所需的时间,也可能会更改b
的值。我就是这样做的,只需定义一个可以访问外部作用域的toggleA
函数:
var a = 0, b, t,
toggleA = function() {
switch(a) {
case 0:
a = 1;
t = window.setTimeout(function(){ b = 1; }, 500);
break;
case 1:
window.clearTimeout(t);
a = b = 0;
break;
}
};
调用
toggleA()
将a
s值切换到1和0之间。根据将a
的值从1切换到0所需的时间,也可能会更改b
的值。我就是这样做的,只需定义一个可以访问外部作用域的toggleA
函数:
var a = 0, b, t,
toggleA = function() {
switch(a) {
case 0:
a = 1;
t = window.setTimeout(function(){ b = 1; }, 500);
break;
case 1:
window.clearTimeout(t);
a = b = 0;
break;
}
};
调用
toggleA()
将a
s值切换到1和0之间。根据将a
的值从1切换到0所需的时间,也可能会更改b
的值。我就是这样做的,只需定义一个可以访问外部作用域的toggleA
函数:
var a = 0, b, t,
toggleA = function() {
switch(a) {
case 0:
a = 1;
t = window.setTimeout(function(){ b = 1; }, 500);
break;
case 1:
window.clearTimeout(t);
a = b = 0;
break;
}
};
调用
toggleA()
将a
s值切换到1和0之间。根据将a
的值从1切换到0所需的时间,也可以更改b
的值。如果可以,请使用定义属性包装访问:
var obj = {
_a: 1
};
Object.defineProperty(obj, "a", {
get: function() {
return this._a;
},
set: function(newA) {
if (this.changeB) {
clearTimeout(this.changeB);
this.changeB = null;
}
if (this.a == 0 && newA == 1) {
this.changeB = setTimeout(function() {
this.b = 1;
}.bind(this), 500);
}
else if (this.a == 1 && newA == 0) {
this.b = 0;
}
this._a = newA;
}
});
然后,您可以这样使用它:
// Immediately set to 0
obj.a = 0;
console.log(obj.b);
// Set to 1 and start the timeout
obj.a = 1;
console.log(obj.b);
setTimeout(function() {
console.log(obj.b);
// Set back to 0
obj.a = 0;
console.log(obj.b);
// And hey, make sure changing a stops b from being set
obj.a = 1;
obj.a = 2;
setTimeout(function() {
console.log(obj.b);
}, 500);
}, 500);
如果可以,请使用defineProperty
包装访问:
var obj = {
_a: 1
};
Object.defineProperty(obj, "a", {
get: function() {
return this._a;
},
set: function(newA) {
if (this.changeB) {
clearTimeout(this.changeB);
this.changeB = null;
}
if (this.a == 0 && newA == 1) {
this.changeB = setTimeout(function() {
this.b = 1;
}.bind(this), 500);
}
else if (this.a == 1 && newA == 0) {
this.b = 0;
}
this._a = newA;
}
});
然后,您可以这样使用它:
// Immediately set to 0
obj.a = 0;
console.log(obj.b);
// Set to 1 and start the timeout
obj.a = 1;
console.log(obj.b);
setTimeout(function() {
console.log(obj.b);
// Set back to 0
obj.a = 0;
console.log(obj.b);
// And hey, make sure changing a stops b from being set
obj.a = 1;
obj.a = 2;
setTimeout(function() {
console.log(obj.b);
}, 500);
}, 500);
如果可以,请使用defineProperty
包装访问:
var obj = {
_a: 1
};
Object.defineProperty(obj, "a", {
get: function() {
return this._a;
},
set: function(newA) {
if (this.changeB) {
clearTimeout(this.changeB);
this.changeB = null;
}
if (this.a == 0 && newA == 1) {
this.changeB = setTimeout(function() {
this.b = 1;
}.bind(this), 500);
}
else if (this.a == 1 && newA == 0) {
this.b = 0;
}
this._a = newA;
}
});
然后,您可以这样使用它:
// Immediately set to 0
obj.a = 0;
console.log(obj.b);
// Set to 1 and start the timeout
obj.a = 1;
console.log(obj.b);
setTimeout(function() {
console.log(obj.b);
// Set back to 0
obj.a = 0;
console.log(obj.b);
// And hey, make sure changing a stops b from being set
obj.a = 1;
obj.a = 2;
setTimeout(function() {
console.log(obj.b);
}, 500);
}, 500);
如果可以,请使用defineProperty
包装访问:
var obj = {
_a: 1
};
Object.defineProperty(obj, "a", {
get: function() {
return this._a;
},
set: function(newA) {
if (this.changeB) {
clearTimeout(this.changeB);
this.changeB = null;
}
if (this.a == 0 && newA == 1) {
this.changeB = setTimeout(function() {
this.b = 1;
}.bind(this), 500);
}
else if (this.a == 1 && newA == 0) {
this.b = 0;
}
this._a = newA;
}
});
然后,您可以这样使用它:
// Immediately set to 0
obj.a = 0;
console.log(obj.b);
// Set to 1 and start the timeout
obj.a = 1;
console.log(obj.b);
setTimeout(function() {
console.log(obj.b);
// Set back to 0
obj.a = 0;
console.log(obj.b);
// And hey, make sure changing a stops b from being set
obj.a = 1;
obj.a = 2;
setTimeout(function() {
console.log(obj.b);
}, 500);
}, 500);
您是否使用了一种功能,以便能够准确地知道a
何时更改?例如,您正在使用setter或使用defineProperty
?您是否使用了一种功能,以便您能够准确地知道a
的更改时间?例如,您正在使用setter或使用defineProperty
?您是否使用了一种功能,以便您能够准确地知道a
的更改时间?例如,您正在使用setter或使用defineProperty
?您是否使用了一种功能,以便您能够准确地知道a
的更改时间?例如,您正在使用setter或使用defineProperty
?