Javascript 我可以做一个变量';s值取决于对另一个变量的更改?

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

我有一个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) {
    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