1

In code below, I expect variable counter to incement by 1 on each repetition, but it remains zero.

var count = 0;
var marqueeText = new Array("Some text goes here",
                            "A bit more verbose text goes here.",
                            "Some more verbose <a href='xxx'>text</a> goes <a href='xxx'>here</a> with some <a href='xxx'>links</a>."
                            );

function fnShowMarquee(count){  
// console.log(count + "-" + marqueeText.length );
        if (count > marqueeText.length){
            count = 0;      
        }
        else{           
            count++;
        }
        $('#marquee_box span').html(marqueeText[count]);

            // do some stuff, then wait 4 seconds and call function again

        setTimeout ("fnShowMarquee(count)", 4000);
}


$(document).ready(function() {
    fnShowMarquee(0);
});
1

5 Answers 5

3

The problem is you are using a global count then creating a new local in fnShowMarquee

Try this instead:

var count = 0;

//...

function fnShowMarquee(){  
    //...
    setTimeout (fnShowMarquee, 4000);
}

$(document).ready(function() {
    fnShowMarquee();
});

EDIT:- From RobG's comment:

$(document).ready(function() {
    var count = 0;
    //...

    function fnShowMarquee(){ ... }
    fnShowMarquee();
});

by putting it all in the ready function the count variable cannot be accessed by other code and it will not mess around in the window global scope.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, but better to hold count in a closure so it isn't global and able to be messed around by other code.
@RobG Thats right! It will still be global the the closure scope and won't affect outside code. Or it can be moved into the ready function.
2

Further to James' answer, keep count and marqueeText in closures so they can't be messed around by other code:

var fnShowMarquee = (function() {
    var count = 0;
    var marqueeText = [
       "Some text goes here",
       "A bit more verbose text goes here.",
       "Some more verbose <a href='xxx'>text</a>" + 
       " goes <a href='xxx'>here</a> with some <a href='xxx'>links</a>."
    ];
    // ...                           

    return function() {  
        //...
        setTimeout (fnShowMarquee, 4000);
    }
}());

Comments

1

I'd try using count explicitly:

setTimeout (function() { fnShowMarquee(count); }, 4000);

8 Comments

@MustaphaGeorge Why bother passing it in at all? It's already a global.
why not setTimeout(function(){fnShowMarquee(count);}, 4000) -- no need for creating a string
@vol7ron - because it can be just:setTimeout(fnShowMarquee, 4000) since count is global.
@RobG: that's if count is global. This is only a snippet, so it's possible he's running this elsewhere. I would advise not to pollute the global namespace.
Variable count can be anywhere on the scope chain, see other answers. It was global in the OP, you didn't change it, so that's where it is for now (though it should be removed as a formal parameter in the declaration of fnShowMarquee).
|
1

Made a few changes to your code:

  1. You are using a global count variable. You don't need to pass it in the parameters.
  2. The if condition should be count == marqueeText.length - 1. In your previous code, marqueeText[count] was going off the bounds.
  3. $('#marquee_box span') wasn't working for me. So I changed it to $('span#marquee_box')

Your code should be:

var count = 0;
var marqueeText = new Array("Some text goes here",
                            "A bit more verbose text goes here.",
                            "Some more verbose <a href='xxx'>text</a> goes <a href='xxx'>here</a> with some <a href='xxx'>links</a>."
                            );

function fnShowMarquee(){  
  console.log(count + "-" + marqueeText.length );
  if (count == marqueeText.length - 1){
      count = 0;      
  }
  else{           
      count++;
  }
  $('span#marquee_box').html(marqueeText[count]);

      // do some stuff, then wait 4 seconds and call function again

  setTimeout ("fnShowMarquee()", 4000);
}


$(document).ready(function() {
    fnShowMarquee();
});

Comments

0

You are increasing the parameter variable count. Not the global one. And the setTimeout like that uses the global one, because it runs your function on document scope.

Try to call your function as just

function fnShowMarquee()

or do it like

setTimeout (function() { fnShowMarquee(count); }, 4000);

1 Comment

Don't pass strings to setTimeout, pass functions instead.

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.