javascript 函数未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13787110/
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 19:47:31  来源:igfitidea点击:

javascript function not defined

javascripthtml

提问by Cloud Strife

I set the function called "drawEverything()" to run onload, however the function doesn't run and I get a bug in Firebug saying "drawEverything is not defined"?? Thing is, I did define it!

我将名为“drawEverything()”的函数设置为在加载时运行,但是该函数没有运行,并且我在 Firebug 中遇到一个错误,提示“drawEverything 未定义”?问题是,我确实定义了它!

I googled the problem and most of the time it was due to some kind of syntax error, but I've looked all over it and can't find any syntax errors.

我在谷歌上搜索了这个问题,大部分时间是由于某种语法错误,但我已经查看了所有内容并找不到任何语法错误。

My code is as follows:

我的代码如下:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
<link rel=stylesheet href="style.css" type="text/css">
</head>

<script type="text/javascript">

var new_x = 110;
var new_y = 150;
function drawEverything(){
    var temp_x = new_x;
    var temp_y = new_y;

    var c = document.getElementById("myCanvas");
    var ctx = c.getContext("2d");
    for (int i = 0; i < 5; i++){
        ctx.beginPath();
        ctx.arc(temp_x,temp_y,20,0,2*Math.PI);

        ctx.stroke();
        if ((i < 3) || (i == 4)){
            temp_x += 40;
        }
        else if (i == 3){
            temp_y += 20;
            temp_x -= 60;
        }
    }
}
</script>

<body onload = "drawEverything()">
<h1>Interactive Olympic Rings</h1>
<div id="container">
<canvas id="myCanvas" width="300" height="300"></canvas>
</div>

</body>
</html>

Any help would be appreciated, thanks!

任何帮助将不胜感激,谢谢!

回答by Quentin

Deal with your errors in order. Later errors can be a consequence of earlier one.

按顺序处理你的错误。较晚的错误可能是较早错误的结果。

First error (as reported by firebug):

第一个错误(由 firebug 报告):

SyntaxError: missing ; after for-loop initializer
[Break On This Error]   

for (int i = 0; i < 5; i++){

Second error:

第二个错误:

ReferenceError: drawEverything is not defined

Since the function definition has compile time errors in it, it is not defined when you come to run it.

由于函数定义中存在编译时错误,因此在您运行它时并未定义它。

You want varnot int.

你想var没有int

回答by Akhil Sekharan

You cannot use int here. Use var in javascript

你不能在这里使用 int 。在 JavaScript 中使用 var