简体   繁体   中英

Call javascript function and obtain return value with anonymous function

I saw some JavaScript calling function like this:

 $(hr).executeService('CCTV','CCTV',{'NVRAssetKey':nvrak,'cklst':cklst},function(data)
          {  
              _ws = data[0]['WebServer'];
              _cams = data[0]['Cameras'];
         //...
         }
);

How to create this type of function to get return value from the anonymous function ?

What I want to do :

 Calculate( 1,2,function(sum){
         console.log(sum);
    });

I tried this code but not worked.

<script>
            function doIt(param1) {
                doIt2(param1, function (data) {
                    console.log(data);
                });
            }

            function doIt2(param) {
                return param;
            }

</script>
<button onclick="doIt(Math.random());">Click</button>

Are you looking for this?

 <script> function Calculate(a, b, callback) { var result = a + b; return callback(result); } function doIt2(a, b) { Calculate(a, b, function(data) { console.log(data); }); } </script> <button onclick="doIt2(5,10);">Click</button> 

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