0

2 つの変数を比較したいと思いoldRefreshますnewRefresh。入力内のoldRefreshの値なので、入力するだけで簡単に保存できoldRefreshます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変数に値を取得するにはどうすればよいですか?

4

2 に答える 2

1

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-13T13:59:16.760 に答える
-1

これを行うと、値を比較できるはずです。

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

返されるデータは、サーバーからの応答である必要があります。

于 2013-08-13T14:04:40.047 に答える