-2

我有这样的代码:

<script>
function GetData(json)
{
.......

code1()
[or]
code2()
[or]
code3()

}
</script>

我想GetData用任何选项(code1、code2、code3)打电话。

例如:如何在 code1GetDatacode2()code3 被以下选项破坏时调用:

<script>
code2();
document.write("<script src=\".....callback=GetData\"><\/script>");
</script>

谢谢你的帮助。

4

2 回答 2

2

阅读您之前的问题,并尝试了解您的确切需求,我相信有两种方法可以解决您的问题:

1. 在你的回调函数中添加另一个参数
这个页面有一个很好的回调示例,但我相信你已经理解了这部分。

在您的代码中,您定义function GetData(json). 也许你应该把它改成

function GetData(json, callbackfunc) {
  ..
  // Here, instead of any logic for code1() or code2() etc, you just
  callbackfunc();
  ..
}

并且您需要GetData使用新的函数参数修改您已经调用的位置。

2. 使用全局变量来定义您需要
的内容 如果由于某种原因无法传递附加参数,您可能需要为回调定义一个变量。例子:

 <script type="text/javascript">

   var mynextcallback;

   function GetData(json) {
     ..
     mynextcallback();
     ..
   }       

 </script>

在你打电话的部分GetData,你可以做mynextcallback = code1; GetData();

于 2013-10-15T04:36:26.313 回答
1

尝试以下小提琴:http: //jsfiddle.net/KJSU5/

var code2 = function(){
    alert("This is code2");
};

function GetData()
{

    if(typeof code1 !== 'undefined'){
         code1();
         return;
    }

    if(typeof code2 !== 'undefined'){
         code2();
         return;
    }

    if(typeof code3 !== 'undefined'){
         code3();
         return;
    }
}
于 2013-10-15T04:34:32.450 回答