1

Как брать при перезагрузке страницы, значение из localstorage вместо значения переменной "thisCount"?

    var thisCount = 100;
    function countPeople() {
        const counterNum = document.querySelector('.counter__number');
        var countRandom = Math.floor((Math.random()*10) + 1);
        thisCount = thisCount + countRandom;
        // var localValue = localStorage.setItem('key', thisCount);
        console.log(thisCount);
    }
    setInterval(() => countPeople(), 1000);

1 ответ 1

2
let localStorageItem = localStorage.getItem('key');
let thisCount = localStorageItem !== null ? localStorageItem  : 100;
3

Ваш ответ

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

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