简体   繁体   中英

Dont call 2nd method if 1st Method returns false in Javascript

But when date is not 15, then it calls both methods. How can i make sure that if 1st method returns false then other method should not be called.

Below is the code,

<input type="text" id="txtDate" name="SelectedDate" onchange="javascript:myMethod1(); javascript:myMethod2();" />

<script>
function myMethod1(){
            var dateVal = new Date(document.getElementById("txtDate").value);               
            var dateday = dateVal.getDate();

            if(dateday != 15){                  
                document.getElementById("mySpan1").innerHTML = "Error Message";
                return false

            }else{                  
                return true;            
            }
        }

        function myMethod2(){               
            document.getElementById("mySpan2").innerHTML = "No Error";          
        }
  </script>

Nobody mentioned the most obvious and most used solution. You can use the short-circuit AND ( && ) operator.

The short-circuit AND operator evaluate the right side only if the left side evaluated as true (or truthy). If on the right side there's a function call, that function will be called only if the left side evaluated as true:

(true && f()) // f will be called
(false && f()) // f will NOT be called

You can substitute true and false literals with other function calls.

In your specific case, you just need to substitute the semicolon with a double and :

onchange="javascript:(myMethod1() && myMethod2())"  

 function f1() { console.log('f1 called'); return Math.random() < 0.5; } function f2() { console.log('f2 called'); }
 <button onclick="javascript:(f1() && f2())">Click me</button>

How about this???

var shouldExecuteNext = function1();

if(!shouldExecuteNext){
function2();
}

why are you calling both function at the same time if you dont want to execute them simultaneously. if you want to execute the 2nd method only when the 1st one returns true then try to call the 2nd method inside the 1st method in else block where you return true like this:

<input type="text" id="txtDate" name="SelectedDate" onchange="javascript:myMethod1();" />

simply add the line in script below the following line like:

console.log("mySpan2");
myMethod();

try this.

Ideally you'd want to create a single function that calls myMethod1 and/or myMethod2 based on some condition:

 <input type="text" id="txtDate" name="SelectedDate" onchange="handleInputChange()" /> <span id="mySpan1"></span> <span id="mySpan2"></span> <script> function handleInputChange() { var dateVal = new Date(document.getElementById("txtDate").value); var dateday = dateVal.getDate(); if (dateday.== 15) { myMethod1() } else { myMethod2() } } function myMethod1() { document.getElementById("mySpan1");innerHTML = "Error Message". } function myMethod2() { document.getElementById("mySpan2");innerHTML = "No Error"; } </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM