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

时间:2013-08-15 17:15:10

标签: javascript flash

我想在Action脚本中调用一个javascript函数,如下所示:

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

我希望javascript函数a_js_function接受两个参数,一个是字符串,另一个是回调函数。所以我可以像这样调用js函数:

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

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

这样做的正确方法是什么?

问题解决了,事实证明我传入的第二个是字符串,在javascript中我必须将字符串转换为函数才能调用它。

2 个答案:

答案 0 :(得分:0)

像这样打电话

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

有关详细信息,请参阅this

答案 1 :(得分:0)

由于flash没有对javascript函数another_js_function的任何引用,因此需要将函数名称作为字符串传递,然后在任何名称空间中使用括号访问它。为简单起见,我使用了全局变量,但它可以是任何对象/命名空间

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");

行动中:http://jsfiddle.net/3n1gm4/Np3bW/