Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <input type='text' id='search' >
</body>
</html>
 
function throttle(func, delay) {
  var last, deferTimer
  return function() {
    var _this = this
    var _args = arguments
    var now = +new Date()
    if(last && now < last + delay) {
      clearTimeout(deferTimer)
      deferTimer = setTimeout(function() {
        last = now
        func.apply(_this, _args)
      }, delay)
    }else {
      last = now
      func.apply(_this, _args)
    }
  }
}
function ajax(value) {
  console.log('ajax request ' + value)
}
var throttleAjax = throttle(ajax, 1000)
var input = document.getElementById('search')
input.addEventListener('keyup', function(e) {
  throttleAjax(e.target.value)
})
Output

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers