0

I'm trying to work out how to make this counter work. After a certain amount of animations have played, I want to make the page refresh. I know my code sucks. I'm not educated in this and I'm very new, not to mention it being difficult to concentrate (to say the least) where I'm staying at the moment... so be nice. Here it is:

$(document).ready(function () {
    function loop() {
        var p = 0;
        if (p = 3) {
            location.reload(true);
        } else {
            $("#p3").delay("1000").fadeIn("slow");
            $("#p3").delay("1000").fadeOut("slow", loop);
            p + 1;
        };
        loop();
    });
4
  • Try to repro your issue jsfiddle.net Commented Feb 3, 2013 at 12:02
  • I tried to make it work there but I'm doing something wrong, and I don't know what it is. I think the basic principal of the counter is OK but I'm doing something wrong and I don't have the experience to know what it is... Commented Feb 3, 2013 at 12:04
  • My point about repro is that it'll help you understand what code you need to include inside your question. For instance, what element is #p3? Commented Feb 3, 2013 at 12:05
  • Ahh sorry, I thought that would confuse things. #P3 is just a paragraph that appears at a certain time, it has no relation to the variable P, which is just a counter to refresh the page after three loops of the fadein code. The actual FadeIn, FadeOut code on the page I'm using is much longer, this is a shortened version. There are around ten paragraphs that I'm using in this way on my webpage. Commented Feb 3, 2013 at 12:10

2 Answers 2

2

Your if (p = 3) statement is using the assignment operator = instead of the comparison operator === or ==. So p gets assigned to 3, the result of which is truthy, so the else statement is never executed.

Also your p variable is declared inside your loop() function, so it gets reset every time the function is called - you could move that declaration to just before the function (keep it inside the document ready handler: no need to make it global).

Also the line p + 1; doesn't do anything: it doesn't increment p because you'd need to assign the result back to p with p = p + 1, the shorthand for which is p += 1 or just p++.

Finally, your code as posted has a syntax error: you are missing the closing } from the loop() function. I would guess the intention is to end the function and then call it, so:

$(document).ready(function () {
    var p = 0;  // <--- moved outside function
    function loop() {
        if (p === 3) {   // <-- changed = to ===
            location.reload(true);
        } else {
            $("#p3").delay("1000").fadeIn("slow");
            $("#p3").delay("1000").fadeOut("slow", loop);
            p++; // <-- changed from p + 1
        };
    } // <--- this is the missing bracket
    loop();
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for taking the time to write all this out. It's very appreciated. Have an upvote and a correct answer!
1

I've made some assumptions and written what I believe is what you want, code is un-tested.:

Change it to:

$(document).ready(function () {
    var globalP = 0;
     //this is called when fadeOut completes.
    function fadeComplete() {
       if (globalP == 3) {//if it is 3 reload..
            location.reload(true);
        } else {
            globalP++;//increment counter 
            animate();//start animation again...
        }
    }
    function animate() {
       //start fading in...
       $("#p3").delay("1000").fadeIn("slow", function() {
           //start fading out when the fadeIn completes.
            //should this happen? Since you're fading in the SAME element.
          $("#p3").delay("1000").fadeOut("slow", fadeComplete);
       });
    };
    animate();
});

1 Comment

Thanks very much for taking your time to answer this question. I'm going with the above answer just because it's a bit simpler :D but thanks anyway, this would also work!!

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.