0

I'm getting error with my function change_slide(). ReferenceError: change_slide is not defined on line 24:1. My code is here:

$(document).ready(function() {

    function change_slide() {
        number = pclass.charAt(6);
        number = parseInt(number);

        if (number == 5) number = 1;
        else number++;

        $('.picks').removeClass(pclass);
        pclass = 'bgpick' + number;
        $('.picks').addClass(pclass);
    }

    var random = Math.floor((Math.random() * 5) + 1);
    var pclass = 'bgpick' + random;
    var number;
    $('.picks').addClass(pclass);
    setInterval('change_slide()', 3000);
    $('.next').on('click', function() {
        change_slide();
    });
});

change_slide() on click .next works, in setInterval doesn't work.

naveen
  • 53,448
  • 46
  • 161
  • 251
Stwosch
  • 602
  • 1
  • 7
  • 20

2 Answers2

5

When you use a character string argument to setInterval, the Javascript code is executed in the global environment. You should pass a function reference instead of a character string, then it will be evaluated in the local environment.

setInterval(change_slide, 3000);

Note that you should not put parentheses after change_slide here. That will call the function immediately, instead of passing a reference to the function.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Take the function declaration out of document ready function,

Also change

setInterval('change_slide()', 3000);

To

setInterval(change_slide, 3000);

Here is modified code

$(document).ready(function() {
  var random = Math.floor((Math.random() * 5) + 1);
  var pclass = 'bgpick' + random;
  var number;
  $('.picks').addClass(pclass);
  setInterval(change_slide, 3000);
  $('.next').on('click', function() {
    change_slide();
  });
});
function change_slide() {
  number = pclass.charAt(6);
  number = parseInt(number);

  if (number == 5) number = 1;
  else number++;

  $('.picks').removeClass(pclass);
  pclass = 'bgpick' + number;
  $('.picks').addClass(pclass);
}
M.Tanzil
  • 1,987
  • 1
  • 12
  • 28
  • The first sentence is the correct answer. The rest of this answer is not needed. – Jonathan M Aug 12 '16 at 19:22
  • No, taking the function out of the `ready` will create a global. Don't fall into this trap. The solution is to not use a string, so that it isn't tied to global scope. – 4castle Aug 12 '16 at 19:24
  • revert _what_ stupidity - the second piece of code is broken. Supporting that is not wise – VLAZ Aug 12 '16 at 19:24
  • @Vld: I got at bit blind. i didnt see it was the invocation. apologies – naveen Aug 12 '16 at 19:28
  • @JonathanM The first sentence fixes it, but it is bad practice. – 4castle Aug 12 '16 at 19:36
  • @4castle, can you explain that a bit? I might agree with you. – Jonathan M Aug 12 '16 at 19:38
  • @JonathanM It creates a global function. [This question](http://stackoverflow.com/q/1841916/5743988) shows how to avoid that practice. Using `.ready()` is another way to avoid making a global. – 4castle Aug 12 '16 at 19:40