How can I make a program wait for a variable change in javascript?

前端 未结 10 1699
耶瑟儿~
耶瑟儿~ 2020-12-01 03:23

I want to force a JavaScript program to wait in some particular points of its execution until a variable has changed. Is there a way to do it? I have already found an extens

相关标签:
10条回答
  • 2020-12-01 04:05

    What worked for me (I looked all over the place and ended up using someone's jsfiddler / very slightly modifying it - worked nicely) was to set that variable to an object with a getter and setter, and the setter triggers the function that is waiting for variable change.

    var myVariableImWaitingOn = function (methodNameToTriggerWhenChanged){
        triggerVar = this;
        triggerVar.val = '';
        triggerVar.onChange = methodNameToTriggerWhenChanged;
        this.SetValue(value){
            if (value != 'undefined' && value != ''){
                triggerVar.val = value; //modify this according to what you're passing in -
                //like a loop if an array that's only available for a short time, etc
                triggerVar.onChange(); //could also pass the val to the waiting function here
                //or the waiting function can just call myVariableImWaitingOn.GetValue()
            }
        };
        this.GetValue(){
            return triggerVar.val();
        };
     };
    
    0 讨论(0)
  • 2020-12-01 04:05

    No you would have to create your own solution. Like using the Observer design pattern or something.

    If you have no control over the variable or who is using it, I'm afraid you're doomed. EDIT: Or use Skilldrick's solution!

    Mike

    0 讨论(0)
  • 2020-12-01 04:08

    Alternatively, you can make a function that executes tasks based on the value of its "Static" variables, example below:

    <!DOCTYPE html>
    
    <div id="Time_Box"> Time </div>
    
    <button type="button" onclick='Update_Time("on")'>Update Time On</button>
    <button type="button" onclick='Update_Time("off")'>Update Time Off</button>
    
    <script>
    
    var Update_Time = (function () {     //_____________________________________________________________
    
    var Static = [];             //"var" declares "Static" variable as static object in this function
    
        return function (Option) {
    
        var Local = [];           //"var" declares "Local" variable as local object in this function
    
            if (typeof Option === 'string'){Static.Update = Option};
    
            if (Static.Update === "on"){
            document.getElementById("Time_Box").innerText = Date();
    
            setTimeout(function(){Update_Time()}, 1000);    //update every 1 seconds
            };
    
        };
    
    })();  
    
    Update_Time('on');    //turns on time update
    
    </script>
    
    0 讨论(0)
  • 2020-12-01 04:12

    JavaScript is one of the worst program\scripting language ever!

    "Wait" seems to be impossible in JavaScript! (Yes, like in the real life, sometimes waiting is the best option!)

    I tried "while" loop and "Recursion" (a function calls itself repeatedly until ...), but JavaScript refuses to work anyway! (This is unbelievable, but anyway, see the codes below:)

    while loop:

    <!DOCTYPE html>
    
    <script>
    
    var Continue = "no";
    setTimeout(function(){Continue = "yes";}, 5000);    //after 5 seconds, "Continue" is changed to "yes"
    
    while(Continue === 'no'){};    //"while" loop will stop when "Continue" is changed to "yes" 5 seconds later
    
        //the problem here is that "while" loop prevents the "setTimeout()" to change "Continue" to "yes" 5 seconds later
        //worse, the "while" loop will freeze the entire browser for a brief time until you click the "stop" script execution button
    
    </script>
    

    Recursion:

    <!DOCTYPE html>
    
    1234
    
    <script>
    
    function Wait_If(v,c){
    if (window[v] === c){Wait_If(v,c)};
    };
    
    Continue_Code = "no"
    setTimeout(function(){Continue_Code = "yes";}, 5000);    //after 5 seconds, "Continue_Code" is changed to "yes"
    
    Wait_If('Continue_Code', 'no');
    
        //the problem here, the javascript console trows the "too much recursion" error, because "Wait_If()" function calls itself repeatedly!
    
    document.write('<br>5678');     //this line will not be executed because of the "too much recursion" error above!
    
    </script>
    
    0 讨论(0)
提交回复
热议问题