[removed] How do constantly monitor variables value

前端 未结 6 1046
青春惊慌失措
青春惊慌失措 2021-02-14 08:35

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:51

    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.

提交回复
热议问题