title: Rails/ jQuery - searching live through database
body: <p>I implemented a simple search while I feed to my database, so I can see if the record with the name already exists:</p>
  
  <pre><code>function doneTyping() {
    $.ajax({
      url: $('#existance_route').val(),
      type: "GET",
      dataType: "json",
      data: { place_name: $name_input.val() },
      complete: function() {},
      success: function(data, textStatus, xhr) {
        setupTextfieldsAddonsWith(data);
        console.log("Response received!");
      },
      error: function() {
        console.log("AJAX error!");
      }
    });
  };
  </code></pre>
  
  <p><code>setupTextfieldsAddonsWith(data)</code> just simply tell the user (by the UI) that a records exists or not.</p>
  
  <p>However I have an issue. Sometimes when I go to the link to create a new record, it just doesn't work. But then I reload the page manually and it starts working.</p>
  
  <p>Does anyone have any idea why I receive such behavior?</p>
  
  <p>PS. If there are "wiser" plugins for doing the thing that I describe please let me know. I only found Client Side Validations but it is now exactly what I need to do</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>This is how I trigger doneTyping (code from one of the SO questions):</p>
  
  <pre><code>var typingTimer;
  var doneTypingInterval = 500;
  // place_name is an id bind to a text_field
  $('#place_name').on('keyup', function() {
    clearTimeout(typingTimer);
    typingTimer = setTimeout(doneTyping, doneTypingInterval);
  });
  
  $('#restaurant_name').on('keydown', function () {
    clearTimeout(typingTimer);
  });
  </code></pre>