重複の可能性:
JavaScriptでvarを使用する場合と使用しない場合の違い
これら2つのアプローチの主な違いは何ですか。誰かが私を理解するのを手伝ってくれませんか。変数が関数内で「var」キーワードで宣言されているとはどういう意味ですか?どちらの場合も、タイマーは予想どおり3秒でクリアされます。
Approach 1:
<html>
<body>
<script type="text/javascript">
function function2()
{
int=self.setInterval(function(){
alert(int);
clearInterval(alert("in clear"+ int));
},3000);
}
</script>
<button onclick="function2()">Start</button>
</body>
</html>
Approach 2
<html>
<body>
<script type="text/javascript">
function function2()
{
var int=self.setInterval(function(){
alert(int);
clearInterval(alert("in clear"+ int));
},3000);
}
</script>
<button onclick="function2()">Start</button>
</body>
</html>