reactjs - Throttling on keypress events in Javascript - Stack Overflow

I have a react application, I have an event listener set so that when a user types, an autosave functio

I have a react application, I have an event listener set so that when a user types, an autosave function is triggered. The trouble is, I don't want api calls being fired on each event. Ideally I'd want it to poll the API every 3 seconds or so when a user is typing.

I have the following code currently:

 window.setTimeout(function() {
  window.setInterval(function() {
    console.log(data);
    this.actions.autoSave(data); 
  }, 3000);
}, 3000);

As you can imagine that's not quite what I'm after. So I just wondered what the best approach would be?

I have a react application, I have an event listener set so that when a user types, an autosave function is triggered. The trouble is, I don't want api calls being fired on each event. Ideally I'd want it to poll the API every 3 seconds or so when a user is typing.

I have the following code currently:

 window.setTimeout(function() {
  window.setInterval(function() {
    console.log(data);
    this.actions.autoSave(data); 
  }, 3000);
}, 3000);

As you can imagine that's not quite what I'm after. So I just wondered what the best approach would be?

Share Improve this question asked Sep 5, 2015 at 11:59 Ewan ValentineEwan Valentine 3,9618 gold badges46 silver badges72 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 6

This is a generic throttle function:

var functionName = (function () {
    'use strict';

    var timeWindow = 500; // time in ms
    var lastExecution = new Date((new Date()).getTime() - timeWindow);

    var functionName = function (args) {
         // your code goes here
    };

    return function () {
        if ((lastExecution.getTime() + timeWindow) <= (new Date()).getTime()) {
            lastExecution = new Date();
            return functionName.apply(this, arguments);
        }
    };
}());

if you connect it to your case lets say the user types in a textarea and you saved a variable for it named userText.

you can do this:

userText.addEventListener("keyup", function(e){
    functionName(userText.innerText);
},false);

this will throttle the calls to your api, if you added your logic to the inner function with the ment // your code goes here.

Example debounce from https://github./Terebinth/Vickers/blob/master/lib/vickers__workshop.coffee

set_boundingRect: ->
    @forceUpdate()
    bounding_rect = React_DOM.findDOMNode(@).getBoundingClientRect()
    @setState
        view_width: bounding_rect.width
        view_height: bounding_rect.height
        x: bounding_rect.width / 2 # transform coordinate system
        y: bounding_rect.height / 2 # translation of coordinate



debounce: (func, wait, immediate) ->
    timeout = 'scoped here'
    ->
        context = @
        args = arguments
        later = ->
            timeout = null
            if not(immediate) then func.apply(context, args)
        callNow = immediate and not(timeout)
        clearTimeout(timeout)
        timeout = setTimeout(later, wait)
        if callNow then func.apply(context, args)
debounced_set_boundingRect: -> @debounce(@set_boundingRect, 100)()

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745193739a4615988.html

相关推荐

  • reactjs - Throttling on keypress events in Javascript - Stack Overflow

    I have a react application, I have an event listener set so that when a user types, an autosave functio

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信