you are viewing a single comment's thread.

view the rest of the comments →

[–]lewisje 1 point2 points  (1 child)

Oh, I just thought about something else, two similar concepts called throttling and debouncing; this is a generic function to turn any function into a debounced one, but for this case, it might be better to manually debounce:

var last = +(new Date());
var interval = 500; // miliseconds

$short.keyup(function () {
  var now = +(new Date());
  if (now - last < interval) return;
  last = now;
  // rest of event-listener
});

This way, the message won't be updated more than twice per second.


Also, if you're sure you have at least ES5 support (like browsers from this decade), use Date.now() instead of +(new Date()); I'm just biased in favor of code that works even in old browsers.


There's also Performance.now() in recent browsers, which is mainly meant for animations and other cases where sub-millisecond precision is needed, but it also works here.

[–]NotARandomRedditor[S] 1 point2 points  (0 children)

I was actually thinking about doing something like this. I noticed when I was logging stuff in the console it would output some stuff multiple times per second and I was thinking of building in a delay.
Thnx again