Si8 Si8 - 1 year ago 69
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
function but using the class that was sent as the selector of the function but it's not working. I keep getting
as the console log.

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

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download