注意:匿名函数调用直接加载函数名setInterval(move,30)
setInterval要求第一個(gè)參數(shù)必須是含Javascript命令的字符串或函數(shù)對(duì)象,所以
setInterval("move()",300)
以及
setInterval(move,300)
這兩個(gè)都是正確的。
而
setInterval(move(),300)
當(dāng)Javascript運(yùn)行到這個(gè)語(yǔ)句時(shí),會(huì)立即執(zhí)行move這個(gè)函數(shù),然后把函數(shù)的返回值作為setInterval的第一個(gè)參數(shù),而由于move函數(shù)沒(méi)有返回值,實(shí)際就相當(dāng)于
setInterval(null, 300)
這個(gè)當(dāng)然就不會(huì)運(yùn)行啦,表面看起來(lái)就是move只運(yùn)行了一次。
move()和move是不相同的,move()是語(yǔ)句,表示要立即執(zhí)行這個(gè)函數(shù)的意思;move則是一個(gè)函數(shù)對(duì)象,代表了這個(gè)函數(shù)本身,本身是不會(huì)運(yùn)行的,可以把它賦值給其他對(duì)象或作為其他函數(shù)的參數(shù)。
?
<html> <body><input type="text" id="clock" size="35" /> <script language=javascript> var int=self.setInterval("clock()",50) function clock(){var t=new Date()document.getElementById("clock").value=t} </script> </form> <button onclick="int=window.clearInterval(int)"> Stop interval</button></body> </html> <!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>選擇器+效果</title><style>#tips{width:200px;height:300px;background: gray;position:absolute;bottom:0px;right:0px;}span{display:inline-block;width:16px;height:16px;line-height:16px;text-align:center;border:1px solid red;margin-left:10px;margin-top:10px;cursor:default;}</style><script src="js/jquery-3.3.1.min.js"></script> </head> <body> <div id="tips"><span>X</span> </div> <script>var f;$('#tips>span').click(function(){$(this).parent().slideUp('slow');f = setInterval(show_div,2000);});function show_div(){$('#tips').slideDown('slow');clearInterval(f);}</script> </body> </html>?
總結(jié)
以上是生活随笔為你收集整理的注意:匿名函数调用直接加载函数名setInterval(move,30)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 身份证号码验证算法
- 下一篇: 三星s9 android p内测,三星开