Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need to pass your already loaded data to server, the server can check and return those that are not loaded yet. Here I also recommend using <code>setTimeout</code> instead of <code>setInterval</code>. Because <code>setInterval</code> <strong><em>will always fire the next call</em></strong> after an interval, inside the function you also use ajax which is also underministic => this could become messy when your server is under heavy load with slow response time. In this function, we should set the next interval <strong><em>only when the current ajax has completed</em></strong>.</p> <pre><code>&lt;script&gt; $(document).ready(function () { refreshComments(); //trigger the first call }); function refreshComments(){ var loadedIds = $("#Urunler li").map(function(){ return $(this).attr("employeeId"); }).get() .join(",")); //get current loaded ids $.ajax({ url: 'WebForm1.aspx?loaded='+loadedIds, success: function (data) { if (data){ //append only when there is a newly added comment. var element = $(data); element.hide(); $("#Urunler").append(element);//use append instead of html because html will replace the old ones. element.slideDown(); setTimeout(refreshComments,5000); } } }); } &lt;/script&gt; </code></pre> <p>Server side code needs to be updated to return the ids and check for the ids</p> <pre><code>using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; namespace WebApplication2 { public partial class WebForm1 : System.Web.UI.Page { SqlConnection cnn = new SqlConnection("Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;"); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //Check for already loaded Ids, I assume that each record in your database has an Id string loadedIds = Request.QueryString["loaded"]; string sql = "SELECT EmployeeId,FirstName FROM Employees"; if (!string.IsNullOrEmpty(loadedIds)){ sql = "SELECT EmployeeId ,FirstName FROM Employees WHERE EmployeeId NOT IN (" + loadedIds +")"; }// You could query it using a safer method than string concatenation, here I just demonstrate the idea cnn.Open(); SqlCommand cmd = new SqlCommand(sql, cnn); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { //This should be a Literal Control, embed Id in the response. Urunler.Text += string.Format("&lt;li employeeId='{0}'&gt;{1}&lt;/li&gt;",dr.GetString(0),dr.GetString(1)); } } cnn.Close(); } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload