You can't use the same variable name for an argument to the function and a global variable and have access to both. The argument named timer
is taking precedence so you are not able to access the global variable also named timer
.
Change the name of the global variable to timerId
and then you can uniquely reference the one you want to reference.
var timerID;
function refreshTimer() {
stopTimer();
timerID = setInterval(postmsg, time*1000);
}
function stopTimer() {
clearInterval(timerID);
}