ajax函数成功后更新变量

时间:2022-03-31 20:21:07

Hey guys quick question, I am sending a request with the jquery ajax function with a variable. After the request I want that variable updated to match the value of the sent back information, so next time the ajax function executes, it send the value of the new variable.
EDIT

嘿伙计们快速提问,我发送了一个带有变量的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);

    });

2 个解决方案

#1


4  

the responce below is correct!

以下的回答是正确的!

just a notation, since you are using countusers also before it change, it assume that you are setting it also before you call AJAX the first time!

只是一个符号,因为你在更改之前也使用了countusers,它假设你在第一次调用AJAX之前也设置了它!

UPDATE:

更新:

<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>

#2


7  

Your defining the variable inside your success function. Define it outside so it has a global scope then update it inside your success function.

您在成功函数中定义变量。在外部定义它,使其具有全局范围,然后在成功函数内更新它。

var countusers = 0;
$.ajax({
   type: "POST",
   data: {"countusers": countusers},
   url: "tester.php",
   dataType: 'json',
   success: function(json){
      countusers=json.rownumber;
   }
});

#1


4  

the responce below is correct!

以下的回答是正确的!

just a notation, since you are using countusers also before it change, it assume that you are setting it also before you call AJAX the first time!

只是一个符号,因为你在更改之前也使用了countusers,它假设你在第一次调用AJAX之前也设置了它!

UPDATE:

更新:

<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>

#2


7  

Your defining the variable inside your success function. Define it outside so it has a global scope then update it inside your success function.

您在成功函数中定义变量。在外部定义它,使其具有全局范围,然后在成功函数内更新它。

var countusers = 0;
$.ajax({
   type: "POST",
   data: {"countusers": countusers},
   url: "tester.php",
   dataType: 'json',
   success: function(json){
      countusers=json.rownumber;
   }
});