简体   繁体   English

调用javascript函数并传递回调函数

[英]Call an javascript function and pass in callback function

I want to call a javascript function in Action script, something like this: 我想在Action脚本中调用javascript函数,如下所示:

ExternalInterface.call('a_js_function', param1, another_js_function);

I want the javascript function a_js_function takes in two params, one is a string, another one is a callback function. 我希望JavaScript函数a_js_function接受两个参数,一个是字符串,另一个是回调函数。 So I can call the js function like this: 所以我可以这样调用js函数:

function a_js_function(testStr, callback) {
    console.log(testStr);
    callback(testStr);
}

function another_js_function(str) {
    console.log(str);
}

What is the correct way to do this? 正确的方法是什么?

Problem solved, it turns out the second I passed in is a string, in javascript I have to turn string into function in order to call it. 问题解决了,原来我传入的第二个字符串是字符串,在javascript中我必须将字符串转换为函数才能调用它。

call like this 这样叫

try {
       ExternalInterface.call('a_js_function', param1, another_js_function);
        } catch(e:Error) {
        trace(e)
        }

and for more information see this 和更多信息请参阅

Since flash does not have any reference to the javascript function another_js_function you need to pass the function name as a string then access it using brackets on whichever namespace. 由于Flash没有对javascript函数another_js_function引用,因此您需要将函数名称作为字符串传递,然后使用任何名称空间上的方括号对其进行访问。 I used global variables for simplicity but it can be any object/namespace 为了简单起见,我使用了全局变量,但是它可以是任何对象/名称空间

another_js_function = function(testStr) {
    alert(testStr);
}

a_js_function = function(testStr, callback) {
    console.log( this.window );
    window[callback].call(this, testStr); // pass scope?
    // OR
    window[callback](testStr);
}
// Simulating call from Flash
a_js_function("howdy y'all", "another_js_function");

In Action: http://jsfiddle.net/3n1gm4/Np3bW/ 实际应用中: http//jsfiddle.net/3n1gm4/Np3bW/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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