嘿伙计们快速提问,我发送了一个带有变量的jquery ajax函数的请求.在请求之后我希望更新的变量与发送的返回信息的值匹配,因此下次执行ajax函数时,它会发送新变量的值.
编辑
$(document).ready(function()
{
var countusers='<?php echo $countusers; ?>';
function refresh() {
$.ajax({
type: "POST",
data: "action=refresh_topic&countusers=" + countusers,
url: "tester.php",
dataType: 'json',
success: function(json)
{
var countusers=json.countusers;
}
})
}
setInterval(refresh, 3000);
});
Run Code Online (Sandbox Code Playgroud)
您在成功函数中定义变量.在外部定义它以使其具有全局范围,然后在成功函数内更新它.
var countusers = 0;
$.ajax({
type: "POST",
data: {"countusers": countusers},
url: "tester.php",
dataType: 'json',
success: function(json){
countusers=json.rownumber;
}
});
Run Code Online (Sandbox Code Playgroud)
下面的回答是正确的!
只是一个符号,因为你countusers也在它改变之前使用它,它假设你也在第一次调用 AJAX 之前设置它!
更新:
<head>
<script>
$(function() {
function refresh() {
var countusers = $('#count_my_users').text();
$.ajax({
type: "POST",
data: "action=refresh_topic&countusers=" + countusers,
url: "tester.php",
dataType: 'json',
success: function(json)
{
$('#count_my_users').empty().append( json.countusers );
// var countusers=json.countusers;
}
})
}
setInterval(refresh, 3000);
});
</script>
</head>
<body>
<div id="count_my_users"><?php echo $countusers; ?></div>
<!--/ THIS DIV HOLD USER COUNT /-->
</body>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3378 次 |
| 最近记录: |