// 左闭右开 能取到 0 但是取不到 1 中间的一个随机小数 [0 ,1)
console.log(Math.random())// 0~ 10 之间的整数
console.log(Math.floor(Math.random()*11))//let arr =['red','green','blue']let random = Math.floor(Math.random()* arr.length)// console.log(random)// console.log(arr[random])// 取到 N ~ M 的随机整数functiongetRandom(N,M){return Math.floor(Math.random()*(M-N+1))+N}
2. 自定义一个随机颜色函数
functiongetRandomColor(flag =true){if(flag){// 3. 如果是true 则返回 #fffffflet str ='#'let arr =['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f']// 利用for循环随机抽6次 累加到 str里面for(let i =1; i <=6; i++){// 每次要随机从数组里面抽取一个 // random 是数组的索引号 是随机的let random = Math.floor(Math.random()* arr.length)// str = str + arr[random]
str += arr[random]}return str
}else{// 4. 否则是 false 则返回 rgb(255,255,255)let r = Math.floor(Math.random()*256)// 55let g = Math.floor(Math.random()*256)// 89let b = Math.floor(Math.random()*256)// 255return`rgb(${r},${g},${b})`}}// 2. 调用函数 getRandomColor(布尔值)
console.log(getRandomColor(false))
console.log(getRandomColor(true))
console.log(getRandomColor())
3. 封装一个倒计时时间戳
// 函数封装 getCountTimefunctiongetCountTime(){// 1. 得到当前的时间戳const now =+newDate()// 2. 得到将来的时间戳const last =+newDate('2023-4-1 18:30:00')// console.log(now, last)// 3. 得到剩余的时间戳 count 记得转换为 秒数const count =(last - now)/1000// console.log(count)// 4. 转换为时分秒// h = parseInt(总秒数 / 60 / 60 % 24) // 计算小时// m = parseInt(总秒数 / 60 % 60); // 计算分数// s = parseInt(总秒数 % 60); // let d = parseInt(count / 60 / 60 / 24) // 计算当前秒数let h =parseInt(count /60/60%24)
h = h <10?'0'+ h : h
let m =parseInt(count /60%60)
m = m <10?'0'+ m : m
let s =parseInt(count %60)
s = s <10?'0'+ s : s
console.log(h, m, s)// 5. 把时分秒写到对应的盒子里面
document.querySelector('#hour').innerHTML = h
document.querySelector('#minutes').innerHTML = m
document.querySelector('#scond').innerHTML = s
}// 先调用一次getCountTime()// 开启定时器setInterval(getCountTime,1000)