效果:
代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<input type="text" id="inpBox"/>
</body>
<script type="text/javascript">
var debounce = function (idle, action) {
var last
return function () {
var ctx = this,
args = arguments
clearTimeout(last)
last = setTimeout(function () {
action.apply(ctx, args)
}, idle)
}
}
$("#inpBox").on('input propertychange', debounce(1000, function () {
var val = $(this).val()
console.log('%cval:'+val,'color:yellow;background:green;')
}))
</script>
</html>复制代码
转载于:https://juejin.im/post/5b6bdedee51d451c4e2a87dc