[removed] How do constantly monitor variables value

前端 未结 6 2402
无人共我
无人共我 2021-02-14 08:36

How do I constantly check a variables value. For example:

if(variable == \'value\'){
    dosomething();
}

This would work if I constantly loope

相关标签:
6条回答
  • 2021-02-14 08:47
    Object.defineProperty(Object.prototype, 'watch', {
        value: function(prop, handler){
            var setter = function(val){
                return val = handler.call(this, val);
            };
            Object.defineProperty(this, prop, {
                set: setter
            });
        }
    });
    

    How to use:

    var obj = {};
    
    obj.watch('prop', function(value){
        console.log('wow!',value);
    });
    
    obj.prop = 3;
    
    0 讨论(0)
  • 2021-02-14 08:50

    As @Pekka commented, you can have a timer constantly poll the variable. A better solution, if it's all your code that's changing the variable, is to not just set the variable directly, but rather have all setters call a function. The function could then set the variable and do any additional processing you need.

    function setValue(value) {
        myVariable = value;
        notifyWatchers();
    }
    
    0 讨论(0)
  • 2021-02-14 08:53

    This solution use deprecated APIs. Computed properties and proxies are a better alternative except on the oldest browsers. See K2Span's answer for an example of how to use those.

    Object.watch:

    Watches for a property to be assigned a value and runs a function when that occurs.

    Object.watch() for all browsers? talks about cross-browser ways to do Object.watch on browsers that don't support it natively.

    0 讨论(0)
  • 2021-02-14 09:00

    Use setInterval:

    var key = ''
    setInterval(function(){
      if(key == 'value'){
        dosomething();
      }
    }, 1000);
    
    0 讨论(0)
  • 2021-02-14 09:05

    If you encapsulate your variable so that the value can only be set by calling a function, it gives you the opportunity to check the value.

    function ValueWatcher(value) {
        this.onBeforeSet = function(){}
        this.onAfterSet = function(){}
    
        this.setValue = function(newVal) {
            this.onBeforeSet(value, newVal)
            value = newVal;
            this.onAfterSet(newVal)
        }
        this.getValue = function() {
            return value;
        }
    }
    
    var name = new ValueWatcher("chris");
    
    wacthedName.onBeforeChange = function(currentVal, newVal) {
        alert("about to change from" + currentVal + " to " + newVal);
    }
    
    name.setValue("Connor");
    
    0 讨论(0)
  • 2021-02-14 09:07

    I had a similar problem and was able to eliminate it using a timed function that I had used earlier. Even if you don't have a timed function there are easy to create;

    var rand = 0
    setInterval(function senseConst () {if (rand = 0) {rand = x}, 10);
    //x could be equal to the variables value that you want to check
    

    I used this to constantly have a variable that is the length of the page by making using the following code

    var widthSensorOutput = 0
    setInterval(function senseConst () {if (widthSensorOutput = 0) {widthSensorOutput = document.getElementById('widthSensor').clientWidth}, 10);
    //'widthSensor' is a div with the width equal to 100%
    

    I am not sure if this is the best way to solve your problem but it worked for me. To be clear, this is the the same basic code that Chandu gave, just I added what you should do once you are inside the function, which already is pretty obvious. I did not understand Chandu's post and did not realize that they used the same root code.

    0 讨论(0)
提交回复
热议问题