简体   繁体   English

如何使用.load()在变量中存储值;

[英]How to store value in variable with .load();

I want to compare two variables, oldRefresh and newRefresh . 我想比较两个变量oldRefreshnewRefresh the oldRefresh 's value in an input so easily storing it in oldRefresh by typing var oldRefresh= $('#oldrefresh').val(); 输入中的oldRefresh值,因此可以通过键入var oldRefresh= $('#oldrefresh').val();轻松地将其存储在oldRefresh var oldRefresh= $('#oldrefresh').val();

but the newRefresh , is hard to get it, I need to get it from another file with .load(); 但是newRefresh很难获取,我需要使用.load();从另一个文件获取它.load();

this is the code: 这是代码:

var oldRefresh= $('#oldrefresh').val();

setInterval(function ()
{
    $('#noti_number').load('include/js_notification_count.php?n=".$_SESSION['username']."');
    });
}, 5000); 

I tried this: 我尝试了这个:

var newRefresh = setInterval(function ()
{
    $('#noti_number').load('include/js_notification_count.php?n=".$_SESSION['username']."');
    });
}, 5000); 
alert(newRefresh);

the result of this is 2 when the result of the load it should be 0 . 当负载的结果应为0时,其结果为2

so I tried this 所以我尝试了这个

setInterval(function ()
{
    var newRefresh = $('#noti_number').load('include/js_notification_count.php?n=".$_SESSION['username']."');
    });
    alert(newRefresh);
}, 5000); 

the result of this is [object Object] . 结果是[object Object] I don't get it. 我不明白 how do I get the load value into a variable? 如何将load值转换为变量?

The jQuery load is replacing the object with the information returned from your js_notification_count.php file. jQuery的负载是用js_notification_count.php文件返回的信息替换对象。 You could add .text() or change load function like: 您可以添加.text()或更改加载功能,例如:

setInterval(function () {
   $('#noti_number').load('include/js_notification_count.php?n=<?=$_SESSION['username']?>', function(response, status, xhr) {
         newRefresh = response;
         alert(newRefresh);
      }
   });
}, 5000);

I would use ajax instead though (if you don't need noti_number to have the returned response) like: 我会使用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); 

if you do this you should be able to compare the values: 如果这样做,您应该能够比较这些值:

$('#noti_number').load(
   'include/js_notification_count.php?n=".$_SESSION['username']."',
   function(aData) {
      //Do your comparison here.
   }
)

The data passed back should be the response from the server. 传回的数据应该是服务器的响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM