Provo a chiamare un altro metodo all'interno di a setTimeout
funzione, ma non trovo il metodo se uso this.install()
dentro setTimeout
. Ho provato alcune soluzioni ma sembra non risolverlo, quindi chiedo qui.
Il codice che ho, ricorda di guardare ai commenti cosa provo a fare:
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();
});
});
risposte:
1 per risposta № 1Dentro il setTimeout
richiama, this
si riferisce al globale - window
oggetto. È possibile salvare un riferimento all'oggetto prima del timeout come var that = this
e utilizzarlo per fare riferimento all'oggetto all'interno del timeout,
oppure puoi passare il contesto usando il bind()
metodo come:
setTimeout(function () {
console.log(this)
}.bind(obj), 10);