您好,登錄后才能下訂單哦!
如何在javascript中使用定時器?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
具體如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>定時器的應(yīng)用</title> <style> #Div1 { width: 100px; height: 100px; position: absolute; background-color: red; top: 50px; } </style> </head> <body> <input id="oBtn1" type="button" value="銨鈕"> <div id="Div1"></div> <script> var oBtn = document.getElementById("oBtn1"); var oDiv = document.getElementById("Div1"); //var timer=null; //oDiv.timer=null; oBtn.onclick = function() { clearInterval(oDiv.timer) oDiv.timer = setInterval(function() { var speed = parseInt(getStyle(oDiv, "left")) + 9; if(speed > 800) { speed = 800; } oDiv.style.left = speed + "px" if(speed == 800) { clearInterval(oDiv.timer); //alert(speed) } }, 50) } function getStyle(obj, attr) { return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj)[attr] } </script> </body> </html>
關(guān)于如何在javascript中使用定時器問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。