我想比较两个变量,oldRefresh和newRefresh。通过键入oldRefresh,输入oldRefresh中的var oldRefresh= $('#oldrefresh').val();值,从而很容易地将其存储在var oldRefresh= $('#oldrefresh').val();中。
但是newRefresh很难得到,我需要用.load();从另一个文件中获得它
这是代码:
var oldRefresh= $('#oldrefresh').val();
setInterval(function ()
{
$('#noti_number').load('include/js_notification_count.php?n=".$_SESSION['username']."');
});
}, 5000); 我试过这个:
var newRefresh = setInterval(function ()
{
$('#noti_number').load('include/js_notification_count.php?n=".$_SESSION['username']."');
});
}, 5000);
alert(newRefresh);其结果是2,当加载的结果应该是0。
所以我试了一下
setInterval(function ()
{
var newRefresh = $('#noti_number').load('include/js_notification_count.php?n=".$_SESSION['username']."');
});
alert(newRefresh);
}, 5000); 其结果是[object Object]。我还是不明白。如何将load值转换为变量?
发布于 2013-08-13 13:59:16
jQuery加载将对象替换为从js_notification_count.php文件返回的信息。您可以添加.text()或更改加载函数,如:
setInterval(function () {
$('#noti_number').load('include/js_notification_count.php?n=<?=$_SESSION['username']?>', function(response, status, xhr) {
newRefresh = response;
alert(newRefresh);
}
});
}, 5000);不过,我会使用ajax (如果您不需要noti_number来获得返回的响应),比如:
setInterval(function () {
$.ajax({
type: "GET", //Change to whatever method type you are using on your page
url: "include/js_notification_count.php",
data: { n: "<?=$_SESSION['username']?>" }
}).done(function(result) {
newRefresh = result;
alert(newRefresh);
});
}, 5000); 发布于 2013-08-13 14:04:40
如果这样做,您应该能够比较以下值:
$('#noti_number').load(
'include/js_notification_count.php?n=".$_SESSION['username']."',
function(aData) {
//Do your comparison here.
}
)传回的数据应该是来自服务器的响应。
https://stackoverflow.com/questions/18211059
复制相似问题