<commit_before>$(function() {

  // Prevent the default action for a search submission
  $(".search-form").on("submit", function(event) {
    event.preventDefault();
  });

  // Display update options if update is selected
  $(".search-setting").change(function() {
    if ($(this).val() === "update") {
      $("#status-container").removeClass("hide");
    } else if ($(this).val() === "find") {
      $("#status-container").addClass("hide");
    }
  });


  $(".search-field").on("keypress change input", function() {
    var searchField = $(this);
    var keyword = searchField.val();
    var url = $(".search-form").attr("action");
    var data = "search=" + keyword;
    var currentResults = $("#search-results a").map(function(index, link) {
      var linkId = $(link).attr("href");
      return parseInt(linkId.substring(1, linkId.length));
    });
    if(keyword.length >= 3) {
      var request = $.ajax({
        url: url,
        method: "GET",
        data: data
      });

      request.done(function(response) {

        var newResults = $.map(response, function(index) {
          return index.id;
        });

        var resultsToRemove = $.makeArray(currentResults).filter(function(val) {
          return $.makeArray(newResults).indexOf(val) == -1;
        });
        var resultsToAdd = $.makeArray(newResults).filter(function(val) {
          return $.makeArray(currentResults).indexOf(val) == -1;
        });

        if(response.length > 0) {
          var fadeDelay = 0;
          removeResults(resultsToRemove);
          addResults(resultsToAdd, response);
        } else {
          $("#search-results").html("");
          $("<li class='no-results white'>No results found</li>").hide().appendTo("#search-results").fadeIn(200);
        }
      });
    } else {
      $("#search-results").html("");
    }
  });
});


function removeResults(removeArray) {
  var fadeDelay = 0;
  if (removeArray.length > 0) {
    $.each(removeArray, function(index, playerId) {
      $("#search-results a[href='#"  + playerId + "']").closest("li").delay(fadeDelay).fadeOut(100, function() {
        $(this).remove();
      });
      fadeDelay += 50;
    });
  }
}
function addResults(addArray, response) {
  var fadeDelay = 0;
  if (addArray.length > 0) {
    $(".no-results").remove();
    $.each(response, function(index, player) {
      if (addArray.includes(player.id)) {
        $("<li><a class='white' href='#" + player.id + "'>" + player.name + " " + player.position + " " + player.team + "</a></li>").hide().appendTo("#search-results").delay(fadeDelay).fadeIn(100);
        fadeDelay += 50;
      }
    });
  }
}

var delay = (function(){
  var timer = 0;
  return function(callback, ms){
    clearTimeout (timer);
    timer = setTimeout(callback, ms);
  };
})();<commit_msg>Fix repeated search results bug by just using input as the event trigger<commit_after>$(function() {

  // Prevent the default action for a search submission
  $(".search-form").on("submit", function(event) {
    event.preventDefault();
  });

  // Display update options if update is selected
  $(".search-setting").change(function() {
    if ($(this).val() === "update") {
      $("#status-container").removeClass("hide");
    } else if ($(this).val() === "find") {
      $("#status-container").addClass("hide");
    }
  });


  $(".search-field").on("input", function() {
    var searchField = $(this);
    var keyword = searchField.val();
    var url = $(".search-form").attr("action");
    var data = "search=" + keyword;
    var currentResults = $("#search-results a").map(function(index, link) {
      var linkId = $(link).attr("href");
      return parseInt(linkId.substring(1, linkId.length));
    });
    if(keyword.length >= 3) {
      var request = $.ajax({
        url: url,
        method: "GET",
        data: data
      });

      request.done(function(response) {

        var newResults = $.map(response, function(index) {
          return index.id;
        });

        var resultsToRemove = $.makeArray(currentResults).filter(function(val) {
          return $.makeArray(newResults).indexOf(val) == -1;
        });
        var resultsToAdd = $.makeArray(newResults).filter(function(val) {
          return $.makeArray(currentResults).indexOf(val) == -1;
        });

        if(response.length > 0) {
          var fadeDelay = 0;
          removeResults(resultsToRemove);
          addResults(resultsToAdd, response);
        } else {
          $("#search-results").html("");
          $("<li class='no-results white'>No results found</li>").hide().appendTo("#search-results").fadeIn(200);
        }
      });
    } else {
      $("#search-results").html("");
    }
  });
});

function removeResults(removeArray) {
  var fadeDelay = 0;
  if (removeArray.length > 0) {
    $.each(removeArray, function(index, playerId) {
      $("#search-results a[href='#"  + playerId + "']").closest("li").delay(fadeDelay).fadeOut(100, function() {
        $(this).remove();
      });
      fadeDelay += 50;
    });
  }
}
function addResults(addArray, response) {
  var fadeDelay = 0;
  if (addArray.length > 0) {
    $(".no-results").remove();
    $.each(response, function(index, player) {
      if (addArray.includes(player.id)) {
        $("<li><a class='white' href='#" + player.id + "'>" + player.name + " " + player.position + " " + player.team + "</a></li>").hide().appendTo("#search-results").delay(fadeDelay).fadeIn(100);
        fadeDelay += 50;
      }
    });
  }
}
{'fraction_non_alphanumeric': 0.19733235885254888, 'fraction_numerical': 0.00858761191302759, 'mean_word_length': 2.739071038251366, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2991910', 'n_tokens_mistral': 1689, 'n_tokens_neox': 1643, 'n_words': 320}