For whatever reason, I can’t get this function repeat. Once it gets the setTimeout, it spits out the “uncaught referenceerror: getnumbers is not defined (where getnumbers is just the name of the variable.
$(document).ready(function(){
var getnumbers = {
countDigit: function(){
if (sessionStorage.counter=="NaN"){
sessionStorage.counter="0";
}
else {
sessionStorage.counter=Number(sessionStorage.counter)+1;
}
$("#result").text("counter: " +sessionStorage.counter);
setTimeout("getnumbers.countDigit()",3000);
},
_init: function(){
getnumbers.countDigit();
}
};
getnumbers._init();
})
Ironically, if I refresh the page the counter works, so I know it’s just getting stuck on that one line. What could I be missing?
Thanks!
setTimeoutwith a string argument is just a globaleval. When it tries to evaluategetnumbers.countDigit(), it is evaluated in the global scope, and no longer has access togetnumbers.Solution: Don’t pass in a string. Instead, try this: