2

I have the following javascript code:

// timer code
    var count=120;
    var pars = 0;
    var counter=setInterval(timer, 1000); // will  run it every 1 second
    var redirect="index.php";

    function timer(){
      if (count <= 0){
         window.location = redirect;
      } else{
          if(pars == 1){

          } else {
            count=count-1;
          }
      }
      document.getElementById("timer").innerHTML=":" + count ;
    }

which displays a timer in div#timer, and I need it to have a pause on some link click as follow:

$(".quad").click(function(){
    pars = 1;
});

As I am new to javascript, I think this problem is cause by global & private variables. Can you help me please?

3
  • You may want to look at using the JQuery widget factory, which manages a lot of this for you, as in jqueryui.com/widget and onallthingsweb.wordpress.com/2013/01/05/… Commented Jan 10, 2013 at 1:21
  • what's the actual problem that you are having? do you get an error (if so, what?), or is it just not working as you expect (what is it doing)? Commented Jan 10, 2013 at 1:23
  • The timer function is not pausing even if variable pars is not equal to 1 Commented Jan 10, 2013 at 1:24

3 Answers 3

2

Your code works for me. Make sure your .click event is registered after the document is loaded:

$(document).ready(
    $(".quad").click(function(){
        pars = 1;
    });
});

You should also do the redirect using

window.location.href = redirect;
Sign up to request clarification or add additional context in comments.

3 Comments

The onload event is the key :)
Thanks dude, that gave me the solution :)
Ribot - Dude! You're website's a trip!!
1

It's running OK here, too. This assumes the onload function refered to by others.

It's nicer to use:

      if(pars != 1){
        count=count-1;
      }

Comments

0

Make sure both code snippets are working in the same scope. If you have your click handler code inside of a document.ready callback, move the timer code to the callback too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.