js写倒计时_javascript

<span class="countDown"></span>
<script>
function countDown(second, select) {
let oSpan = document.querySelector("." + select);
if (!!second) {
let val;
second >= 0 ? val = second : val = Math.abs(second);
let countDownTime = setInterval(() => {
if (val < 0) {
clearInterval(countDownTime);
val = "计时结束";
}
oSpan.innerHTML = val;
--val;
}, 1000);
} else if (!!second === false) {
console.log("请传参!");
}
}

countDown(3, "countDown");


</script>