Using "setTimeout ()" in a jQuery expression

I am trying to add a JavaScript function setTimeout()to my jQuery expression. Without setTimeout()my function, it loads a partial view with updated information when entering text in a text field. But it loads information with every keystroke. My logic is that if I can set a timer, then when the user stops typing, a second later the data will be updated.

As you can see, I tried to fit setTimeout(), but it does not seem to work.

$(function() {
    $('#DocId').live('keyup', function() {
        setTimeout(var styleValue = $(this).val();
        $('#tableContent').load(
        '/CurReport/TableResults', 
        { style: $(this).val()}), 1000);

    }),
});

Thank,

Aaron

+3
source share
1 answer

.data() , :

$('#DocId').live('keyup', function() {
  clearTimeout($.data(this, 'timer'));
  var val = $(this).val();
  var wait = setTimeout(function() {
    $('#tableContent').load('/CurReport/TableResults', { style: val });
  }, 1000);
  $(this).data('timer', wait);
});

- 1 , ( ). , , 1 , .

: var val = $(this).val();, , this - , .

+5

Source: https://habr.com/ru/post/1738677/


All Articles