'중지'버튼을 누를 때까지 이미지가 수평으로 이동하는 웹 페이지를 만들었습니다.onload 속성에서 변수를 선언 할 수 없습니다.
<html>
<head>
<script>
var count = 0;
function move(){
image = document.getElementsByTagName("img")[0];
count++;
image.style.left = count;
}
</script>
</head>
<body onload = "var temp = setInterval(move,1)">
<img src = "facebook_icon.png" style = "position:absolute; left = 0; top:0;">
<br>
<button onclick = "clearInterval(temp);">Click here to stop the loop.</button>
</body>
</html>
onload
특성에서 var 키워드를 제거하면 코드가 제대로 실행됩니다. 새 코드 : -
<html>
<head>
<script>
var count = 0;
function move(){
image = document.getElementsByTagName("img")[0];
count++;
image.style.left = count;
}
</script>
</head>
<body onload = "temp = setInterval(move,1)">
<img src = "facebook_icon.png" style = "position:absolute; left = 0; top:0;">
<br>
<button onclick = "clearInterval(temp);">Click here to stop the loop.</button>
</body>
</html>
왜 그렇게 되나요?
'var'도 두 번째 코드에 .. –