Javascirpt - remove rows dynamically after 2mins

Javascirpt - remove rows dynamically after 2mins

i am calling a webapi using get method which will give json data and i am display as a table from that data. the code which i have given below is working fine. i got around 25 rows(dynamically" of data when i fit the service. this webpage will be refreshed using signalR. when any changes in DB immediately it should reflect in the webpage.

my question is if "" + item.TakeupType + "" value is "Completed" or "cancelled" this row background should change to gray color and after 2mins row should be remove from the webpage.


Note: 1) only 22 rows should display on webpage in orderby asc Datetime.
2) Completed/cancelled items from DB should will display on the webpage for 2 mins and drop off.




My code :

  1. //Webapi call - To load info status.
  2. function LoadinfoStatus() {
  3.     $.ajaxSetup({
  4.         beforeSend: function (xhr) {
  5.             xhr.setRequestHeader('x-api-key', '9024024A-024-485C024-6BC2024DA');
  6.         }
  7.     });

  8.     $.ajax({
  9.         type: "GET",
  10.         url: "https://mywebsites.net/version1/info/494",

  11.         contentType: "application/json; charset=utf-8",

  12.         dataType: "json",
  13.         success: function (data) {
  14.             // alert(JSON.stringify(data));
  15.             $("#DIV").html('');
  16.             var DIV = '';

  17.             //Function to get DateFormat
  18.             function getFormattedDate(date) {
  19.                 // debugger;
  20.                 var inputdate = new Date(date);
  21.                 var currentdate = new Date();

  22.                 var strTime = getFormattedTime(inputdate);
  23.                 var rngStrTime = getFormattedTime(add_minutes(inputdate, 5));

  24.                 if (inputdate > currentdate) {
  25.                     return inputdate.getDate() + '/' + (inputdate.getMonth() == 0 ? 12 : inputdate.getMonth()) + " " + strTime + " - " + rngStrTime;
  26.                 }
  27.                 else {
  28.                     return strTime + " - " + rngStrTime;
  29.                     //return day + "/" + month + " - " + strTime;
  30.                 }
  31.             }

  32.             var add_minutes = function (dt, minutes) {
  33.                 return new Date(dt.getTime() + minutes * 60000);
  34.             }

  35.             function getFormattedTime(inputdate) {
  36.                 var day = inputdate.getDate();
  37.                 var month = inputdate.getMonth() + 1;
  38.                 var hours = inputdate.getHours();
  39.                 var minutes = inputdate.getMinutes();
  40.                 var ampm = hours >= 12 ? 'pm' : 'am';
  41.                 hours = hours % 12;
  42.                 hours = hours ? hours : 12;
  43.                 minutes = minutes < 10 ? '0' + minutes : minutes;

  44.                 return hours + ':' + minutes + ampm;
  45.             }


  46.             $.each(data, function (i, item) {
  47.                 

  48.                
  49.                 var rows = "<tr ' " + (item.Count > 100 ? "data-id='" + item.orderId + "'" : "") + (item.TakeupType == "Cancelled" ? "style='background-color: gray; color: white'" : "") + " align= 'center' > " +
  50.                     "" + "" + "" + "" +
  51.                     "" + item.user.firstName + " " + item.user.lastName.charAt(0) + "." + "" +
  52.                     "" + item.TakeupType + "" +
  53.                     "" + (item.Count == undefined ? "$" + " " + 0 : "$" + " " + item.Count) + "" +
  54.                     "" + getFormattedDate(item.TakeupTimeUtc) + "" +
  55.                     "= 100 ? "style='background-color: darkorange; color: white'>***" : ">") + "" +
  56.                     "";

  57.                 var $tbody = $('tblOrders tbody');

  58.                 $('#tblOrders').append(rows);
  59.             }); //End of foreach Loop

  60.             
  61.             registerEvents();
  62.             // console.log(data);
  63.         }, //End of AJAX Success function

  64.         failure: function (data) {
  65.             alert(data.responseText);
  66.         }, //End of AJAX failure function
  67.         error: function (data) {
  68.             alert(data.responseText);
  69.         } //End of AJAX error function

  70.     });
  71. }