Making a notification system using jQuery and Ajax [closed]

倖福魔咒の 提交于 2019-12-12 03:55:28

问题


I need to build a notification system, based on jQuery and Ajax, I have put together some PHP code to display JSON like this:

{ "msg_new": "1", "note_new": "2", "frd_new": "2", "frd_link": "" }

How can I load this 'notification' to <span></span> tags using jQuery and setInterval? I am a little bit confused, can anyone please help me out?

EDIT

Currently, this is the code I've attempted to use, but it doesn't seem to work:

<span id="new_msgs"></span>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
    function get_new() {
        $.getJSON("update_new.php", function(data) {
            $('#new_msgs').html(data["msg_new"]);
        });
    }

    setInterval(get_new, 1000);
</script>

回答1:


Here you go, one example:

HTML:

<span id="result">
  <span id="note"></span>
</span>

JavaScript:

function loadIt() {
  $.get('ajax/test.php', function(data) {
    var jdata = JSON.parse(data);
    $('#result #note').html(jdata.note);
    ...
  });
}
setInterval(loadIt, 1000);

This shows how to hill one if your variables, copy paste and change for the rest Of course test.php returns your json, and 1000 is 1 second in milliseconds.



来源:https://stackoverflow.com/questions/15320270/making-a-notification-system-using-jquery-and-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!