Javascript 每次在jquery中加载网站或更改var时执行函数
我有一个ajax函数,应该在每次加载网站时执行,这确实没有问题,但我使用框架,每次当我的全局变量更改时,该函数也应该执行,但它只执行一次。这是完美的参考代码:Javascript 每次在jquery中加载网站或更改var时执行函数,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有一个ajax函数,应该在每次加载网站时执行,这确实没有问题,但我使用框架,每次当我的全局变量更改时,该函数也应该执行,但它只执行一次。这是完美的参考代码: var buttonClicked = false; //global variable function sync(arg, callback){ $('.loader').show(); $.ajax({ method: 'GET',
var buttonClicked = false; //global variable
function sync(arg, callback){
$('.loader').show();
$.ajax({
method: 'GET',
url: 'sync/active.php',
dataType: 'json',
data:{calling: arg},
success: function(data, status, xhr){
$('.loader').hide();
callback(data);
},
error: function(xhr, ajaxOptions, thrownError){
console.log(thrownError);
}
});
}
这就是问题所在
$( document ).ready(function() { // or if(buttonClicked == true) execute again
sync("patient", function(data) {
var res = data;
for(var i=0; i<res.length;i++){
for(var key in res[i]){
var value = res[i][key];
switch(key){
case "id":
res[i][key] = parseInt(value);
break;
case "kundennr":
res[i][key] = parseInt(value);
break;
case "client":
res[i][key] = value;
break;
case "start":
res[i][key] = new Date(value);
break;
case "end":
res[i][key] = new Date(value);
break;
case "title":
res[i][key] = value;
break;
case "description":
res[i][key] = value;
break;
case "termart":
res[i][key] = parseInt(value);
break;
case "userId":
res[i][key] = parseInt(value);
break;
default:
console.log("error at "+key);
}
}
}
patient = res;
});
}
$(document).ready(函数(){//或如果(buttonClicked==true)再次执行
同步(“患者”),功能(数据){
var res=数据;
对于(var i=0;i