2017-03-21 1 views
0

이 코드를 실행하면 콘솔에 콘솔 로그가 표시되지 않습니다. 메소드를 실행하지 않는 디버깅 방법 (here에서 가져옴)입니까?왜 디버깅 기능이 메소드를 실행하지 않습니까?

function debounce(func, wait, immediate) { 
 
    var timeout; 
 
    var args = Array.prototype.slice.call(arguments, 3); 
 
    return function() { 
 
     var context = this; 
 
     var callNow = immediate && !timeout; 
 
     clearTimeout(timeout); 
 
     timeout = setTimeout(function() { 
 
      timeout = null; 
 
      if (!immediate) { 
 
       func.apply(context, args); 
 
      } 
 
     }, wait); 
 
     if (callNow) func.apply(context, args); 
 
    }; 
 
}; 
 

 
var f1 = function(){ console.log(1) }; 
 
var f2 = function(){ console.log(2) }; 
 
debounce(f1, 100, false); 
 
debounce(f2, 100, false);

이 예상되는 동작 아니면 내가 여기서 뭔가를 놓친거야?

답변

1

debounce 함수가 다른 함수를 반환하기 때문입니다. 당신은 이런 식으로 전화를해야 :

debounce(f1, 100, false)(); 
debounce(f2, 100, false)(); 

function debounce(func, wait, immediate) { 
 
    var timeout; 
 
    var args = Array.prototype.slice.call(arguments, 3); 
 
    return function() { 
 
     var context = this; 
 
     var callNow = immediate && !timeout; 
 
     clearTimeout(timeout); 
 
     timeout = setTimeout(function() { 
 
      timeout = null; 
 
      if (!immediate) { 
 
       func.apply(context, args); 
 
      } 
 
     }, wait); 
 
     if (callNow) func.apply(context, args); 
 
    }; 
 
}; 
 

 
var f1 = function(){ console.log(1) }; 
 
var f2 = function(){ console.log(2) }; 
 
debounce(f1, 100, false)(); 
 
debounce(f2, 100, false)();

+0

감사합니다,이 도움이되었다. 내가 그것을 놓쳤다라고 생각할 수 없다 :) – gurvinder372

관련 문제