如何为该代码添加回调函数

How to add call back function for this code?

本文关键字:添加 回调 函数 代码      更新时间:2023-09-26

我想知道向下面的函数添加回调函数最简单的方法是什么:

<script>
$(document).on('focus', '#inputbox', function(e) {
    $( this).contents().filter(function() {
      return this.nodeType === 3;
    }).wrap("<span class='new'></span>");
//I tried add function here but it would execute infinite times.
});
</script>

与您已经尝试过的相同,但跳过对cb 的所有冗余调用

<script>
function cb() { alert('Wow!'); }
(function() {
var timer;
var delay = 1000; // call cb delay
$(document).on('focus', '#inputbox', function(e) {
    $( this).contents().filter(function() {
      return this.nodeType === 3;
    }).wrap("<span class='new'></span>");
    clearTimeout(timer);
    timer = setTimeout(cb, delay);
});
})();
</script>