How to check if condition for 5 seconds in Javascript

后端 未结 2 571
自闭症患者
自闭症患者 2021-01-17 05:09

How to check an if condition for 5 seconds? This if condition should be true for 5 seconds to execute its code.

function checkcodition() {         


        
2条回答
  •  情书的邮戳
    2021-01-17 05:35

    You should use browser APIs setTimeout function.

    This example will only check the condition AFTER five seconds, not DURING five second interval. If you want to make sure the condition stays true during whole interval, you should probably implement some onValueChange() type event.

    function checkCodition() {
        let a, b;
        setTimeout(() => {
            if (a === 0 && b === 1) {
                console.log(" This line shoud display after 5 seconds");
            }
        }, 5000);
    }
    

    Additional info:

    You should use tripple equality sign ===, use let instead of var and camelCase insted of lowercase for function names.

提交回复
热议问题