Si8 Si8 - 1 month ago 5
HTML Question

Why is passing $(this) as a parameter not working

How come the following line:

pop = setInterval(function () { doSlide($(this)) }, settings.timeoutSet);


not sending the proper reference as the parameter.

I would like to set up the same functionality as the commented out section in the
AutoSlide(theObj2)
function but using the class that was sent as the selector of the function but it's not working. I keep getting
[object][Object]
as the console log.

How can I resolve the issue so the auto sliding function works correctly.

Answer

Timeouts run in the context of window, unless told otherwise, so inside your callback this no longer points to the element, but window instead.

Instead, you can bind the callback to the element, i.e. set its context.

setInterval(function () { AutoSlide($(this)) }.bind(this), settings.timeoutSet);