javascript - jQuery multiple instance plugin using "this" in setTimeout -


i'm trying write new plugin can initialized on multiple elements within same page, each time different options, ex:

$('#id').plugin({ option:true }); $('#id2').plugin({ option:false }); 

i'm using boilerplate jqueryboilerplate.com (https://github.com/jquery-boilerplate/jquery-boilerplate). understand (at least think do!) problem within scope of anonymous function (here, within settimeout) 'this' refers window. in following, output logged first time, not second:

// avoid plugin.prototype conflicts $.extend(plugin.prototype, {     init: function () {         console.log(this.settings.propertyname);         settimeout(function(){             console.log(this.settings.propertyname);         }, 1000);     } }); 

elsewhere this.settings.propertyname set 'value'. console.log result is:

value uncaught typeerror: cannot read property 'propertyname' of undefined 

if example set window.prop = this.settings.propertyname , console.log window.prop instead, works, problem there may many instances running @ same time.

i've read through lots of questions related subject, none seem address particular situation. grateful if give me clear example of how within context of jquery plugin using boilerplate, or it. please excuse noobness, thank you!!

capture closure on this:

$.extend(plugin.prototype, {     init: function () {         var _this = this;         console.log(this.settings.propertyname);         settimeout(function(){             console.log(_this.settings.propertyname);         }, 1000);     } }); 

Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -