---
# System prepended metadata

title: js-math&計時器

---

# js-math&計時器
## 四捨五入round
```script
	let str1=Math.round(4.5)//5
	console.log(str1);
```
## 無條件進位到整數ceil
```script
	let str2=Math.ceil(3.4)//4
	console.log(str2);
```
## 無條件捨去到整數floor
```script
	let str3=Math.ceil(5.7)//5
	console.log(str3);
```
## 次方pow
```script
	let str4=Math.pow(2);
	console.log(str4);
```
## 平方根sqrt
```script
	let str5=Math.sqrt(36);
	console.log(str5);
```
## 立方根cbrt
```script
	let str6=Math.cbrt(27);
	console.log(str6);
```
## 從數列中找出最大、最小值
```script
 	let str7=Math.min(0,1,2,3,4);
 	console.log(str7);
---------------------------------
	let str8=Math.max(0,1,2,3,4);
	console.log(str8);
```
## 從陣列中找出最小值、最大值
```script
	let arr=[1,2,3,4,5];
	let str8=Math.min(...arr);
	console.log(str8);
---------------------------------
	let arr1=[2,3,4,5];
	let str9=Math.max(...arrl);
	console.log(str9);
```
## 隨機Math.random() 0-1 -> *10 -> 1-10
```script
	let str10=Math.random();
	console.log(str10);0-10
	----------------------------
	let rand=Math.round(Math.random()*10);
	console.log(rand); round(0-10) ceil(1-10) floo(0-9)
```
### 練習隨機抽樣
設functionz放置最大數字
    1.先宣告一個陣列放數字
    2.利用迴圈設有幾個數字位置
    3.進行判斷式如果有重復則再抽，可用includes查詢有沒有在裡面
# ==計時器==
## 幾秒後開始
```script 
    setTimeout(()=>{
    document.write("三秒鐘過了");
    },3000);
```
## 每隔多久執行一次
```script 
    let timer = setInterval(()=>{
        document.write("一秒鐘<br>");
    },1000);
```
## 只跑一次
setInterval(funcion,間隔,丟進去的值)
```script 
    let write=(str)=>{
    document.write(str+"<br>");
    }
    let timer2=setInterval(write,1000,"AAA");
```
## 讓計時器停止clearInterval
```script 
    setTimeout(()=>{
    clearInterval(timer);
    },5000)
 ```