更新未定义?
我试图在画布上创建一个移动对象,但每次设置的间隔结束时,它都会给出错误:“更新未定义”,即使更新的定义就在初始化之前。如何设置正确调用更新的间隔?
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test</title>
<meta name="description" content="An HTML5 Test">
<meta name="author" content="William Starkovich">
<link rel="stylesheet" href="css/styles.css?v=1.0">
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var angle = 0.0;
var increment = 0.1;
function incrementAngle(angle, increment){
angle += increment;
if(angle > Math.PI * 2)
angle -= (Math.PI * 2);
}
function update(angle, increment){
incrementAngle(angle, increment);
//get a reference to the canvas
var ctx = $('#canvas')[0].getContext("2d");
//draw a circle
ctx.strokeStyle = "#000000";
ctx.beginPath();
ctx.arc(20, 20, 15, angle, angle + Math.PI, true);
ctx.lineWidth = 5;
ctx.stroke();
ctx.closePath();
}
function init(angle, increment){
update(angle, increment)
setInterval("update(angle, increment)",1000);
}
init(angle, increment);
});
</script>
</head>
<body>
<canvas id="canvas" width="800px" height="100px">
<p>Your browser doesn't support canvas.</p>
</canvas>
</body>
</html>
I'm trying to make a moving object on canvas, but each time the set interval goes off it gives me the error: "update is not defined" even though update's definition is right there before init. How do I get set interval to call update correctly?
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test</title>
<meta name="description" content="An HTML5 Test">
<meta name="author" content="William Starkovich">
<link rel="stylesheet" href="css/styles.css?v=1.0">
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var angle = 0.0;
var increment = 0.1;
function incrementAngle(angle, increment){
angle += increment;
if(angle > Math.PI * 2)
angle -= (Math.PI * 2);
}
function update(angle, increment){
incrementAngle(angle, increment);
//get a reference to the canvas
var ctx = $('#canvas')[0].getContext("2d");
//draw a circle
ctx.strokeStyle = "#000000";
ctx.beginPath();
ctx.arc(20, 20, 15, angle, angle + Math.PI, true);
ctx.lineWidth = 5;
ctx.stroke();
ctx.closePath();
}
function init(angle, increment){
update(angle, increment)
setInterval("update(angle, increment)",1000);
}
init(angle, increment);
});
</script>
</head>
<body>
<canvas id="canvas" width="800px" height="100px">
<p>Your browser doesn't support canvas.</p>
</canvas>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎正在寻求使用全局变量,那么为什么不一路走下去呢?
http://jsfiddle.net/mblase75/8L62c/3/
You appear to be looking to use global variables, so why not go all the way?
http://jsfiddle.net/mblase75/8L62c/3/
不要在
eval
中使用字符串,因为它们是在全局范围内执行的,并且您的函数是在 document.ready 处理程序中定义的。如果您想提供参数,请使用匿名函数:Don't use strings in
eval
because they are executed in global scope and your function is defined in the document.ready handler. Use an anonymous function if you want to provide arguments: