调用函数A(args)调用函数B()然后调用函数A(args),如何做?

时间:2010-03-20 15:47:57

标签: javascript function

参见示例:

<!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", {
                    "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>

2 个答案:

答案 0 :(得分:1)

你需要将调用者和参数传递给load_api,否则它怎么知道无关变量的值呢?

        function load_api(f, text) {
         ...

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

然后你可以在回调中调用那些。

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

如果A是可变参数,则可以使用arguments变量和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);
  });
}

答案 1 :(得分:1)

如果你不止一次调用google.load,那么很聪明的是不再包含脚本,但你的回调仍然会被调用。因此,您可以这样做:

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.
        });
}

请注意,由于闭包的工作方式,您可以在回调中自由使用函数参数。但是您可能会发现在页面加载时只有一个回调更简单,并且在回调完成之前隐藏任何转换控件。