मैं अंदर एक और विधि को कॉल करने की कोशिश करता हूं setTimeout
कार्य करें, लेकिन यदि मैं उपयोग करता हूं तो यह विधि नहीं ढूंढता है this.install()
के भीतर setTimeout
। मैंने कुछ समाधानों की कोशिश की है, लेकिन इसे हल करने के लिए नहीं लगता है, इसलिए मैं यहां पूछता हूं।
मेरे पास कोड, टिप्पणियों को देखने के लिए याद रखें कि मैं क्या करने की कोशिश करता हूं:
jQuery(window).load(function () {
$.widget( "testing.updater", {
options: {
},
_create: function() {
//
this.element.addClass("text");
//
this.downloadFiles("bootstrap");
},
downloadFiles: function(packagen) {
var ajax = $.ajax({
type: "POST",
url: "responses/test.php",
data: "download=" + packagen,
success: function(data) {
var obj = jQuery.parseJSON( data);
$(".text").append("Downloading files...<br>");
$("#updateBtn").attr("disabled", true);
if (obj.downloaded) {
setTimeout(function(message){
$(".text").append(obj.message+"<p><p>");
// Down here I want to call another method
// like this.install(); <.. but ain"t working..
}, 5000);
} else {
return $(".text").append("<p><p> <font color="red">"+obj.message+"</font>");
}
}
});
},
install: function() {
// I want to run this method inside
// prev method, look above comment
},
});
$("#updateBtn").on("click",function(){
var test = $(".updater").updater();
test.updater();
});
});
उत्तर:
उत्तर № 1 के लिए 1के अंदर setTimeout
वापस कॉल करें, this
वैश्विक को संदर्भित करता है - window
वस्तु। आप टाइमआउट से पहले ऑब्जेक्ट का संदर्भ सहेज सकते हैं जैसे var that = this
और इसका उपयोग टाइमआउट के अंदर ऑब्जेक्ट को संदर्भित करने के लिए करें,
या आप संदर्भ का उपयोग कर संदर्भ पारित कर सकते हैं bind()
विधि जैसे:
setTimeout(function () {
console.log(this)
}.bind(obj), 10);