2

I am hoping that someone can help me to correct some issues that I am having with a custom script. I took over the management of a site and there seems to be an issue with the following code:

/*  jQUERY CUSTOM FUNCTION 
------------------------------ */
jQuery(document).ready(function($) {
$('.ngg-gallery-thumbnail-box').mouseenter(function(){
   var elmID = "#"+this.id+" img";
   $(elmID).fadeOut(300);
});

$('.ngg-gallery-thumbnail-box').mouseleave(function(){
   var elmID = "#"+this.id+" img";
   $(elmID).fadeIn(300);
});

var numbers = $('.ngg-gallery-thumbnail-box').size();

function A(i){
    setInterval(function(){autoSlide(i)}, 7000);
}
A(0);

    function autoSlide(i) {
        var numbers = $('.ngg-gallery-thumbnail-box').size();
        var elmCls = $("#ref").attr("class");
        $(elmCls).fadeIn(300);
        var randNum = Math.floor((Math.random()*numbers)+1);
        var elmClass = ".elm"+randNum+" img";
        $("#ref").attr("class", elmClass);
        $(elmClass).fadeOut(300);
        setInterval(function(){arguments.callee.caller(randNum)}, 7000);
    }
});

The error that I am seeing in the console on Firebug is "TypeError: arguments.callee.caller is not a function. I am just getting started with jQuery and have no idea how to fix this issue.

Any assistance with altering the code so that it still works but doesn't throw up all of these errors (if I load the site and let it sit in my browser for 10 minutes I have over 10000 errors in the console) would be greatly appreciated!

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • Probably due to browser upgrade. Take a look at this: http://stackoverflow.com/q/103598/2454376 – mishik Jun 27 '13 at 16:14
  • You have `A` that calls `autoSlide` in an interval, but then `autoSlide` calls *itself* in an interval! Why do you have 2 intervals? – gen_Eric Jun 27 '13 at 16:29

1 Answers1

4

Inside the setInterval, arguments is probably not what you think it is. When called, every function has its own arguments array. When setInterval is ran, the function passed to it is called, so arguments is for that function (the anonymous one).

I see you're trying to use arguments.callee.caller to get the function that called the interval, autoSlide in this case. That won't work. The function passed to setInterval is not called by autoSlide, it's called by the browser. All autoSlide did was call setInterval, so arguments.callee.caller is null.

Since you know the name of the function, autoSlide, you can just use that.

setInterval(function(){
    autoSlide(randNum);
}, 7000);

UPDATE: Are you sure you need a setInterval inside autoSlide in the first place? Your code calls A(0), which calls autoSlide with a setInterval already. Do you really need both intervals?

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • 2
    To put it another way (for the OP's benefit) functions invoked by `setInterval` are run with a clean [call stack](https://en.wikipedia.org/wiki/Call_stack). – apsillers Jun 27 '13 at 16:22
  • @apsillers: Yep, exactly. I know what I'm talking about, but I'm not the best at explaining :-) – gen_Eric Jun 27 '13 at 16:23
  • 1
    Well, I think with 59k rep, you're not *that* bad at explaining :) – apsillers Jun 27 '13 at 16:24