0

как можно сделать таймер на обновлении страницы? Чтобы стартовое значение задавалось через атрибут. Должно быть написано на чистом JS.

Еще вылазит ошибка: Uncaught TypeError: timerSet.hasAttribute is not a function

window.onload = function() {
  let timerSet = document.getElementsByClassName('timer');
  let timer;
  let refresh = document.getElementsByClassName('btn-refresh');

  if (!timerSet.hasAttribute('data-timer')) {
    timerSet.setAttribute('data-widget-timer', '2 : 00');
  } else {
    timer = timerSet.getAttribute('data-timer');
  }

  refresh.onclick = function() {
    window.location.reload();
  }
};
.refresh {
  width: 150px;
}

.btn-refresh {
  padding: 10px 20px;
  background-color: gray;
  border: none;
  outline: none;
  cursor: pointer;
  color: #fff;
  font-size: 15px;
}

.timer {
  width: 100px;
  margin: 20px auto;
  color: red;
}
<div class="refresh">
  <button class="btn-refresh">Обновить страницу</button>
  <div class="timer" data-timer="5 : 00">
    3 : 35
  </div>
</div>

7
  • document.getElementsByClassName возвращает коллекцию а не один элемент 8 ноя 2019 в 13:07
  • @StrangerintheQ как выбрать 1 элемент?
    – Imp3l
    8 ноя 2019 в 13:08
  • document.getElementsByClassName[0] или document.querySelector('.class') 8 ноя 2019 в 13:10
  • или через цикл обработать коллекцию :)
    – ccxdec
    8 ноя 2019 в 13:16
  • 1
    meta refresh не в почете ныне?
    – teran
    8 ноя 2019 в 13:39

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Посмотрите другие вопросы с метками или задайте свой вопрос.