jquery - Finding a previous value returned via AJAX -


i'm trying finish who's online script running issues repeating usernames auto refresh function. there way in jquery find previous values returned , if found, skip showing duplicate usernames?

here code:

$(document).ready(function() {     $.ajax({         type: "get",         url: "/members/groups/get-one-group-members-online/" + location.pathname.split("/")[4],         datatype: "json",     }).done(function(msg) {         $.each(msg.display_name, function(i, item) {             $("#members-online").append(msg.display_name[i] + "<br>");         });     }).fail(function() {         $('#members-online').html("error retrieving online group members");     }); });  function reloadelement() {     $.ajax({         type: "get",         url: "/members/groups/get-one-group-members-online/" + location.pathname.split("/")[4],         datatype: "json",     }).done(function(msg) {         $.each(msg.display_name, function(i, item) {            // how not show repeating values?            $("#members-online").append(msg.display_name[i] + "<br>");         });     }).fail(function() {         $('#members-online').html("error retrieving online group members");     }); }  setinterval(function() {     reloadelement() }, 5000); 

and actual html element:

<p id="members-online"></p> 

any appreciated. can try , add more information if question unclear.

thanks!

given you're performing exact same logic in both situations logic can simplified. instead of appending element when call made, rebuild whole element either calling empty() on first, or setting it's html().

if extract logic function can call both on load , every n seconds. try this:

$(function() {   function buildonlinememberlist() {     $.ajax({       type: "get",       url: "/members/groups/get-one-group-members-online/" + location.pathname.split("/")[4],       datatype: "json",     }).done(function(msg) {       var displaynames = msg.display_name.join('<br />');       $("#members-online").html(displaynames);     }).fail(function() {       $('#members-online').html("error retrieving online group members");     });   }    setinterval(function() {     buildonlinememberlist(); // call on interval   }, 5000);   buildonlinememberlist(); // call on load }); 

you should note ajax polling considered anti-pattern. better use websockets update connected clients when new member comes online, or goes offline. name can added or removed needed.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -