level 12
<div><span id="countDownSpan"></span></div>
<div><img id="card" src="" alt="." /></div>
<script>
const cardObject = {
_intervalID: null,
_counter: 0,
_startCount: 0,
_cards: "234567890JQKA".split(""),
_suits: "CSHD".split(""),
getRandomCard: function () {
const index = this.getRandomNumber(0, 13);
if (this._cards[index] == 0) {
return "10";
}
return this._cards[index];
},
getRandomSuit: function () {
const index = this.getRandomNumber(0, 4); // 4
return this._suits[index];
},
loadImages: function () {
// 不需要加#
document.getElementById('card').src = "cards" + this.getRandomCard() + this.getRandomSuit() + ".png";
},
countDown: function () {
// 这里不需要if判断
this._counter--;
this.displayCountDown();
if (this._counter <= 0) {
this.stopCountDownTimer();
this.loadImages();
}
},
displayCountDown: function () {
const span = document.querySelector("#countDownSpan");
// 不需要执行 this.countDown()
span.textContent = this._counter; // 去掉 Number() 也可以的
},
startCountDownTimer: function (start) {
this._startCount = start;
this._counter = this._startCount;
this.displayCountDown();
this._intervalID = setInterval(this.countDown.bind(this), 1000);
},
stopCountDownTimer: function () {
// 没必要在用if了
clearInterval(this._intervalID); // 加上this
},
getRandomNumber: function (min, max) {
let randomNum = Math.random() * (max - min) + min;
return Math.floor(randomNum);
}
};
const timedCard = Object.create(cardObject);
timedCard.startCountDownTimer(5);
</script>
2021年03月02日 02点03分
3
嗷嗷嗷非常感谢!不好意思现在才看到
2021年08月30日 07点08分