我一直在試圖與HTML 5這一簡單的動畫和JavaScript從這裏動畫:超時功能使用JavaScript畫布
http://www.developphp.com/view.php?tid=1262
的代碼follwoing:
<!-- Lesson by Adam Khoury @ www.developphp.com -->
<!-- Watch the video to get code explanation line by line -->
<!-- http://www.youtube.com/watch?v=hUCT4b4wa-8 -->
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
canvas{border:#666 1px solid;}
</style>
<script type="text/javascript">
function draw(x,y){
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.save();
ctx.clearRect(0,0,550,400);
ctx.fillStyle = "rgba(0,200,0,1)";
ctx.fillRect (x, y, 50, 50);
ctx.restore();
x += 1;
var loopTimer = setTimeout('draw('+x+','+y+')',100);
}
</script>
</head>
<body>
<button onclick="draw(0,0)">Draw</button>
<canvas id="canvas" width="550" height="400"></canvas>
</body>
</html>
有隻是一件事我不明白在代碼中:在setTimeout
方法'+'
之前和之後x
和y
做什麼,爲什麼報價是我們ED附上+x+
和+y+?
它將一個字符串與一個變量連接起來。 – 2014-09-22 09:16:47
重複[調用setTimeout()函數](http://stackoverflow.com/questions/3800512/calling-functions-with-settimeout)?? – karan3112 2014-09-22 09:17:36