JavaScriptは、単純な小さなケースをタイマー

  1. タイマー

    			setInterval(function(){
    			},t)
    		每隔t ms执行一次。***要注意,循环执行的***
    
  2. クリアタイマは、
    タイマ周期は、タイマーをクリアする必要があるので、いくつかのケースでは、我々は、それがループを実行しない、実行され、上記と述べました。
    クリアタイマー遅延を達成することができます。

clearInterval(定时器名称);

簡単な例

let sum = setInterval(function(){
	console.log(x);
},100);

let x=9;
steTimeout(function(){
	clearInterval(sum);
},300)

上記のコードは3回を印刷する後9は、印刷を停止します
ここに画像を挿入説明
3、タイマーに小さい場合(円を描くように大円の周りに小さな円を作る)を

		代码部分
<style>
    body{
        margin: 0;
    }
    .round{
        box-sizing: border-box;
        display: flex;
        justify-content: center;
        align-items: center;
        width: 400px;
        height: 400px;
        border:1px solid red;
        border-radius: 50%;
        margin:100px auto 0;
    }
    .point{
        width: 10px;
        height: 10px;
        border:1px solid blue;
        border-radius: 50%;
        background-color: blue;
    }
</style>
<body>
    <div class="round"> 
        <div class="point"></div>
    </div>
    <script>
        let oPoint=document.querySelector(".point");
        let STEP=Math.PI/60;
        let cow=0;

        setInterval(function(){
            oPoint.style.transform="translate("+200*Math.sin(cow)+"px,"+200*Math.cos(cow)+"px)";
            cow+=STEP;
        },1000/60)
    </script>

結果セクション

ここに画像を挿入説明
私は、この静的な画像を挿入しているので、円を描くように大円の周りに小さな円を見ることができませんでした。しかし、それは実際に円を描くようにあります。

出版元の記事 ウォンの賞賛4 ビュー34

おすすめ

転載: blog.csdn.net/HeartzzZZ/article/details/105059148