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