1.3 KiB
1.3 KiB
title | localeTitle |
---|---|
How to Create a Countdown Timer | كيفية إنشاء مؤقت للعد التنازلي |
كيفية إنشاء مؤقت للعد التنازلي
خلق
ابدأ ببناء وظيفة countdownTimer.
`function startCountdown(seconds){ var counter = seconds;
var interval = setInterval(() => { console.log(counter); counter--;
if(counter < 0 ){
// The code here will run when
// the timer has reached zero.
clearInterval(interval);
console.log('Ding!');
};
}, 1000); }; `
إعدام
الآن لبدء تشغيل جهاز ضبط الوقت ، نوفر startCountdown()
مع قيمة عدد كوسيطة تمثل الثواني للعد التنازلي.
startCountdown(10); // Console Output // // 10 // 9 // 8 // 7 // 6 // 5 // 4 // 3 // 2 // 1 // 0 // Ding!
مثال حي
المزيد من الموارد
الطرق المستخدمة: