javascript 为什么说没有定义回调?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10160139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:52:37  来源:igfitidea点击:

Why is it saying callback is not defined?

javascriptjsoncallbackjsfiddle

提问by Tgwizman

I have made a server for a game I am making, and it generates a map for me, but on this test client I am making, it's not working correctly.

我为我正在制作的游戏制作了一个服务器,它为我生成了一张地图,但是在我制作的这个测试客户端上,它无法正常工作。

I add the site as a script, and it's working fine as a script, but when the javascript on jsfiddle is read, it says that the function callback() is not define, but it clearly is, and it should be sending the map object to the console, but it's just erroring out... :/

我将站点添加为脚本,并且它作为脚本运行良好,但是当读取 jsfiddle 上的 javascript 时,它说函数 callback() 没有定义,但它显然是,它应该发送地图对象到控制台,但它只是出错......:/

Here's the link to the JS Fiddle: http://jsfiddle.net/Tgwizman/9QrUF/

这是 JS Fiddle 的链接:http: //jsfiddle.net/Tgwizman/9QrUF/

I hope you guys can figure it out, because I'm totally lost as to why it isn't working...

我希望你们能弄清楚,因为我完全不知道为什么它不起作用......

回答by KARASZI István

In your jsfiddle the javascript callback function is defined after you loaded the map with your javascript include, and at that point your callback is not yet defined.

在您的 jsfiddle 中,javascript 回调函数是在您使用 javascript 包含加载地图后定义的,此时您的回调尚未定义。

Here is a fixed fiddle.

这是一个固定的小提琴

Example source:

示例来源:

<script>
var map;

function callback(cb) {
    map = cb;
    console.log(map);
}
</script>
<script src='http://mmorpg_server.jit.su/?map=0,0'></script>?

回答by Chamika Sandamal

change dropdown value to no wrap (head)

将下拉值更改为 no wrap (head)

screen shot

截屏

http://jsfiddle.net/9QrUF/3/

http://jsfiddle.net/9QrUF/3/

回答by Joseph

The callback must be defined before the map is loaded. otherwise, you are calling an undefined function. external scripts may download in any order, but are executed in the order they are coded into the page.

必须在加载地图之前定义回调。否则,您正在调用未定义的函数。外部脚本可以按任何顺序下载,但按它们编码到页面中的顺序执行。

<script>
    var map;
    //declare callback first before anything uses it
    function callback(cb) {
        map = cb;
        console.log(map);
    }
</script>

//this calls the callback it MUST be loaded after
<script src='http://mmorpg_server.jit.su/?map=0,0'></script>?

回答by qwertymk

It's because jsFiddle doesn't always work perfectly.

这是因为 jsFiddle 并不总是完美地工作。

Here's a fiddle of it working

这是它工作的小提琴

it happens because jsfiddle wraps the script elements in their own wrappers (closures) and therefore can't access any functions declared in them

发生这种情况是因为 jsfiddle 将脚本元素包装在它们自己的包装器(闭包)中,因此无法访问其中声明的任何函数

Never mind, KARASZI István'sand Joseph'sanswers are better

没关系,KARASZI IstvánJoseph 的答案更好

回答by gaurav

<script type="text/javascript">
var map;

function callback(cb)
 {
    map = cb;
    console.log(map);
}
</script>
<script src='http://mmorpg_server.jit.su/?map=0,0'> 
</script>?

Hope this will work !!!

希望这会奏效!!!