setTimeout(一次) setInterval(定時一次) HTML: <!DOCTYPE html> <html> <head> <meta chartset="utf-8"> <title></title> <link rel ="stylesheet" href= "./style.cs ...
setTimeout(一次)
setInterval(定時一次)
HTML:
<!DOCTYPE html> <html> <head> <meta chartset="utf-8"> <title></title> <link rel ="stylesheet" href= "./style.css"> <script src="./man.js"></script> </head> <body> <div id=div1></div> </body> </html>
CSS:
#div1{ width: 100px; height: 100px; background: pink; }
JS:
div1.style.position = 'relative' div1.style.left = 0 let n = 0 let id =setInterver( ()=>{ n = n+1(每次移動1的距離) div1.style.left = n + 'px'//(註意加上單位) if(n>=200){ window.clearInterval(id)//移動了200px時,清除這個功能 } },40)//設置40ms動一次