在ajax请求/setTimeout中执行jQuery小部件方法
我试图在在ajax请求/setTimeout中执行jQuery小部件方法,jquery,ajax,jquery-ui,jquery-ui-widget-factory,Jquery,Ajax,Jquery Ui,Jquery Ui Widget Factory,我试图在setTimeout函数中调用另一个方法,但是如果我使用this.install()insidesetTimeout它找不到该方法。 我尝试过一些解决方案,但似乎没有解决它,所以我在这里问 对于我的代码,请记住查看注释我尝试执行的操作: jQuery(window).load(function () { $.widget( "testing.updater", { options: { }, _create: function()
setTimeout
函数中调用另一个方法,但是如果我使用this.install()
insidesetTimeout
它找不到该方法。
我尝试过一些解决方案,但似乎没有解决它,所以我在这里问
对于我的代码,请记住查看注释我尝试执行的操作:
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();
});
});
jQuery(窗口).load(函数(){
$.widget(“testing.updater”{
选项:{
},
_创建:函数(){
//
this.element.addClass('text');
//
这个.downloadFiles('bootstrap');
},
下载文件:函数(PackageGen){
var ajax=$.ajax({
类型:“POST”,
url:“responses/test.php”,
数据:“下载=”+packagen,
成功:功能(数据){
var obj=jQuery.parseJSON(数据);
$('.text').append('Downloading files…
');
$('#updateBtn').attr(“disabled”,true);
如果(对象已下载){
设置超时(函数(消息){
$('.text').append(obj.message+'');
//下面我想调用另一个方法
//类似于此。install();在setTimeout
回调中,this
引用全局-窗口
对象。您可以在超时前保存对对象的引用,如var that=this
,并将其用于引用超时内的对象
也可以使用以下方法传递上下文:
setTimeout(function () {
console.log(this)
}.bind(obj), 10);
看看这个问题/答案:我肯定我试过在外部设置一个变量,但没有成功。非常感谢,这很有魅力!!:)