javascript - setInterval keeps giving me undefined function -
javascript - setInterval keeps giving me undefined function -
this question has reply here:
pass right “this” context settimeout callback? 5 answersi have function in code works slider elements within class. want setinterval
on click event keeps giving me function undefined.
here script:
$(document).ready(function(){ $(".slide").click( setinterval( function fadenext() { $(this).children('div:nth(0)').children('img:nth(0)').fadeout().appendto($(this).children("div:nth(1)")); $(this).children("div:nth(1)").children('img:nth(0)').fadein().appendto($(this).children("div:nth(0)")); },1000) ); });
in setinterval functions this
belongs anonymous function, not click one. need pass this
in interval. solution utilize bind on anonymous function
$(document).ready(function () { $(".slide").click(function(){ setinterval(function fadenext() { $(this).children('div:nth(0)').children('img:nth(0)').fadeout().appendto($(this).children("div:nth(1)")); $(this).children("div:nth(1)").children('img:nth(0)').fadein().appendto($(this).children("div:nth(0)")); }.bind(this), 1000) ) }); });
or
$(document).ready(function () { $(".slide").click(function(){ var _this = this; setinterval(function fadenext() { $(_this).children('div:nth(0)').children('img:nth(0)').fadeout().appendto($(this).children("div:nth(1)")); $(_this).children("div:nth(1)").children('img:nth(0)').fadein().appendto($(this).children("div:nth(0)")); }, 1000) ) }); });
javascript jquery
Comments
Post a Comment