2013-07-30 2 views
1

이 익명 함수는 마우스 이동으로 시작됩니다.두 이벤트가 동일한 함수를 실행합니다.

var timeout; 

// onkeypress 

document.onmousemove = function(){ 
    clearTimeout(timeout); 
    timeout = setTimeout(function(){alert("move your mouse");}, 1000); 
} 

키를 눌러도 해고 당할 수 있습니까?

document.onmousemove.namedfunction(); 
document.onkeypress.namedfunction(); 

JSFIDDLE

답변

1

당신은 당신이 지금 그 일을하고 거의 동일한 방법을 수행 할 수 있습니다 또는 유일한 효율적으로 명명 된 함수로 함수를 정의하고 이런 식으로 호출이 작업을 수행하는 방법입니다 :

var timeout; 

document.onmousemove = document.onkeypress = function(){ 
    clearTimeout(timeout); 
    timeout = setTimeout(function(){alert("move your mouse");}, 1000); 
} 

그러나 동일한 함수를 여러 번 재사용하는 것은 이름을 지정하고이를 참조하기위한 이유 일 수 있습니다.

0

당신은 익명을 떠날 수 ...

document.onmousemove = document.onmousemove = function(){ 

을, 나는하지 않을 것입니다.

관련 문제