0

If I want to write in the input 149, the run method 3 times and the variable hour is 1, then 14 and then 149, what I'm looking for is that the changeHour method starts after 3000 ms since I stopped typing

function delay() {
  var timer = 0;
  return function(callback, ms) {
    clearTimeout(timer);
    timer = setTimeout(callback, ms);
  };
}

function changeHour(elem) {
  var id = $(elem).data("Id");
  var hour = $(elem).val();

  API_Loading.show();
  var url = BASE_PATH + "Stage/UpdateHourOfStage";

  API_ajaxWrapper_Library.ajax(url, {
    method: "POST",
    data: {
      id: id,
      hour: hour || 0
    },
    onDone: function(data) {
      API_Loading.hide();
      $("#saveIcon_" + id).show();
      $("#saveIcon_" + id).fadeOut(3000);
    },
    onFail: function(result) {
      API_Loading.hide();
      alert("Error");
    }
  });
}
<div>
  <div class="hour-num" style="background-color:#c4bed0;">Hours to go</div>
  <input class="hour-num decimal" type="text" value="Hour" data-id="Id" onkeyup="delay(changeHour($(this)), 3000)">
</div>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Riccardo Pezzolati
  • 357
  • 1
  • 4
  • 15
  • 1
    What you're looking for is called 'debounce', not delay: https://stackoverflow.com/questions/24004791/can-someone-explain-the-debounce-function-in-javascript – raina77ow Oct 12 '20 at 12:13
  • 2
    [Easily findable](https://www.google.com/search?q=javascript+delay+onkeyup+site%3Astackoverflow.com) – mplungjan Oct 12 '20 at 12:16

0 Answers0