0

I've tried various ways to make sure that my timer variable is global (and I believe it is) but why can't I clear the interval?

var timer;    

function refreshtimer(timer) {
  stoptimer(timer);
  timer = window.setInterval(postmsg, time*1000);
}

function stoptimer(timer) {
  window.clearInterval(timer);
  timer = null;
}

I believe this is the relevant code section; however, the entirety of the code can be found here.

Simon MᶜKenzie
  • 8,344
  • 13
  • 50
  • 77
Shardj
  • 1,800
  • 2
  • 17
  • 43
  • 3
    I don't understand why this question was put on hold. It was very clear what was wrong with the code and all necessary code was disclosed. They used the same variable name for argument and global and thus were never setting the global and thus `stoptimer` never had a chance to work. – jfriend00 May 02 '14 at 00:53
  • @jfriend00, the box below states that the question was put on hold because it "was caused by a problem that can no longer be reproduced or a _simple typographical error_". This seems correct to me. – Simon MᶜKenzie May 02 '14 at 01:24
  • 1
    @SimonMᶜKenzie - this is not a simple typographical error where the OP meant to type something different and accidentally produced this code. This code was written this way on purpose because there was a misunderstanding of how argument names hide the access to global variables. This is a genuine programming problem and the answer teaches about it. Why would SO not want legitimate programming questions with legitimate answers that teach a useful programming concept to understand? This is not a typo. This is a programming mistake and the answer explains/teaches about it. – jfriend00 May 02 '14 at 01:28
  • @jfriend00, fair point, but I'd agree that _this one was resolved in a manner unlikely to help future readers_, as people are unlikely to find this question when searching for variable shadowing problems - perhaps the title needs a rewrite. – Simon MᶜKenzie May 02 '14 at 01:51
  • @SimonMᶜKenzie - is search engine relevance/accuracy a criteria for marking a question as off-topic? I don't think so or more than half the questions here would qualify as off-topic. Besides, most of the search engine relevance would come from my answer anyway. That is not unusual because the questions describe symptoms, often in vague terms and answers tend to cover the actual issue going on in a much more thorough way. I will propose a better title. – jfriend00 May 02 '14 at 01:56
  • @jfriend00, that's definitely a matter of opinion, but personally, I feel that if the question can't be found easily, it won't be of much use to others. While your answer will definitely provide search relevance, I know I'd be unlikely to click on this result based on its original title. In any case, SO is pretty democratic, so I've no doubt this question will be reopened. – Simon MᶜKenzie May 02 '14 at 02:11

1 Answers1

4

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);
}
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • You can. Except bad things happen. – bjb568 May 02 '14 at 00:38
  • @bjb568 There was an implied "and expect things to work..." at the end of that. :) – ErikE May 02 '14 at 00:39
  • I realise that, but I was just throwing anything in there that I hoped would make it work. I don't think removing the arguments helps but I'll try it again – Shardj May 02 '14 at 00:39
  • Ok well im an idiot, Thanks for the help! :) I can't believe that fixed it. Ill tick this answer as soon as it lets me – Shardj May 02 '14 at 00:41
  • Thanks, Its all fixed now though, and those functions are needed separately for the rest of the program but yes you don't need to call stoptimer() :) but thanks for your time, I was stuck on this for an hour and a half some how – Shardj May 02 '14 at 00:44