Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I'm new to jQuery, let me know if I'm doing it right. So here's my code. I nested this function in another function. I want it to execute (pretty much just toggling images) until a count of 3 then continue to execute the rest of the parent function.

//parent function part 1
    $(function flip(){
        var count=1
        if (count==3) {
            return var count=1;
        } else {
            $('#i1').toggle(2);
            $('#i1').toggle(2);
            var count=count+1
        });
//parent function part 2
share|improve this question
3  
Are you sure your code is correct? return var count=1; looks fishy –  fge Jun 8 '13 at 4:52
    
haha I have no idea but I'll delete that. new to jQuery –  Zack Chan Jun 8 '13 at 4:56
1  
Where is the loop 0.o? –  Ganesh RJ Jun 8 '13 at 5:02

2 Answers 2

declare var count=1 out of the function flip
this is because each time the function is called count becomes one and the if part will be never executed

share|improve this answer

you say that you want to execute until a count of 3, you can easily use loop like this :

var count = 1
while(count <= 3){
 // your code here 
 $('#i1').toggle(2); // etc ...
 // .....
 count++; 
}
count = 1;

about your code there is some problems :

//parent function part 1
$(function flip(){
    var count=1
    if (count==3) {
        count=1; // <--- no var here because if you use var you are declaring a new variable, and why you use return ?? if you want juste to reset your count variable we don't need any "return" !
    };
    else {
        $('#i1').toggle(2);
        $('#i1').toggle(2);
        count=count+1  // <--- no var here because if you use var you are declaring a new variable
    } // end of if else statment

 }  //  <---- add this to close your function declaration block
 );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.