Files
freeCodeCamp/guide/russian/javascript/tutorials/how-to-create-a-countdown-timer/index.md
2018-10-16 21:32:40 +05:30

62 lines
1.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: How to Create a Countdown Timer
localeTitle: Как создать таймер обратного отсчета
---
## Как создать таймер обратного отсчета
### Создание
Начните с создания функции countdownTimer.
```javascript
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()` значением в качестве аргумента, представляющим секунды для обратного отсчета.
```javascript
startCountdown(10);
// Console Output //
// 10
// 9
// 8
// 7
// 6
// 5
// 4
// 3
// 2
// 1
// 0
// Ding!
```
### Живой пример
[Codepen - таймер обратного отсчета](https://codepen.io/rdev-rocks/pen/jLogxY?editors=0012)
### Дополнительные ресурсы
Используемые методы:
* [setInterval ()](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval)
* [clearInterval ()](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/clearInterval)