T-Walker Coding
T-Walker Coding

Reputation: 3

clearInterval isn't functioning properly

This is my code that auto adds a certain amount "points" or gold every second depending a varable but when that variable variable changes which is done by a button that runs this command to stop the set Interval isn't functioning. Please help me figure this out.

function addMiner() {
    clearInterval(autoClick)
    if (localStorage.getItem('minercount') > 0) {
        var autoClick = setInterval(function() {
        miner()
        }, 1000);
        document.getElementById("goldNumber").innerHTML = localStorage.getItem('clickcount');
    }
}

Upvotes: 0

Views: 41

Answers (1)

Scimonster
Scimonster

Reputation: 33409

You need to rescope your autoClick variable so that it's accessible where you need it.

var autoClick; // define globally
function addMiner() {
    clearInterval(autoClick)
    if (localStorage.getItem('minercount') > 0) {
        autoClick = setInterval(function() { // set here without redefining
        miner()
        }, 1000);
        document.getElementById("goldNumber").innerHTML = localStorage.getItem('clickcount');
    }
}

Upvotes: 1

Related Questions