开发者

called function A(args) calls a function B() which then calls a function A(args), How to do that?

开发者 https://www.devze.com 2022-12-23 19:47 出处:网络
See example: <!DOCTYPE html> <html> <head> <title>language</title> <script type=\"text/javascript\" src=\"http://www.google.com/jsapi\">

See example:

<!DOCTYPE html>
<html>

    <head>
        <title>language</title>
        <script type="text/javascript" src="http://www.google.com/jsapi">
        </script>
    </head>

    <body>

        <div id="language"></div>

        <script type="text/javascript">

            var loaded = false;

            function load_api() {

                google.load("language", "1开发者_JAVA百科", {
                    "nocss": true,
                    "callback": function() {
                        loaded = true;
                        callback_to_caller(with_caller_agruments); 
                        // how to call a function (with the same arguments) which called load_api() ???
                        // case 1 should be: detect_language('testing');
                // case 2 should be: translate('some text');                       
                    }
                });
            }

            function detect_language(text) {
                if (!loaded) {
                    load_api();
                } else {
                    // let's continue... believe that google.language is loaded & ready to use
                    google.language.detect(text, function(result) {
                        if (!result.error && result.language) {
                            document.getElementById('language').innerHTML = result.language;
                        }
                    });
                }
            }

            function translate(text) {
                if (!loaded) {
                    load_api();
                } else {
                    // let's continue...
                }
            }

            detect_language('testing'); // case 1
            translate('some text'); // case 2
        </script>
    </body>

</html>


You need to pass the caller and arguments to load_api, otherwise how could it know the values of the irrelevant variables?

        function load_api(f, text) {
         ...

        function detect_language(text) {
            if (!loaded) {
                load_api(detect_language, text);  // or use load_api(arguments.callee, text)

And then you can invoke those in the callback.

                "callback": function() {
                    loaded = true;
                    f(text); 

If A is variadic, you can use the arguments variable and Function.apply:

function A (x, y, z) {
  B(A, arguments); // or B(A, [x, y, z])
}
function B (f, arr) {
  setTimeout(1000, function() {
    f.apply(this, arr);
  });
}


If you call google.load more than once, it is smart enough not to include the script again, but your callback will still get called. Thus, you can just do:

function load_api(callback) {

    google.load("language", "1", {
        "nocss": true,
        "callback": callback
    });
}

function detect_language(text) {
    load_api(function() {
        // let's continue... believe that google.language is loaded & ready to use
        google.language.detect(text, function(result) {
            if (!result.error && result.language) {
                document.getElementById('language').innerHTML = result.language;
            }
        });
    });
}

function translate(text) {
        load_api(function()
        {
          // Do whatever you want, including using text.
        });
}

Note that because of the way closures work, you can freely use the function arguments in your callback. But you may find it simpler to just have a single callback at page load, and hide any translation controls until the callback completes.

0

精彩评论

暂无评论...
验证码 换一张
取 消