How to reset timeout in Javascript/jQuery?

前端 未结 2 422
陌清茗
陌清茗 2020-12-16 13:41

I\'ve got a field A in my webpage which, when edited by the user, invokes an API call (using jQuery), which updates field B. After the edit, the API should be called every 1

相关标签:
2条回答
  • 2020-12-16 14:20
    var update;
    
    // something happened
    
    clearTimeout(update);
    update = setTimeout(thisFunction, 10000);
    
    0 讨论(0)
  • 2020-12-16 14:28

    A timer can be cancelled with clearTimeout, so:

    var timer = null;
    if (timer) {
        clearTimeout(timer); //cancel the previous timer.
        timer = null;
    }
    timer = setTimeout(thisFunction, 10000);
    
    0 讨论(0)
提交回复
热议问题