2012-01-28 4 views
2

좋아, 나는 div 클릭을위한 처리기를 설치했다 그러나 어떤 이유로 document.ready에 발사하고, 나는 진짜로 이유를 알아낼 수 없다.내 클릭 핸들러가 문서에서 실행되는 이유는 무엇입니까?

function statusCallback(status){ 
    //THIS IS THE JSONP CALLBACK RECEIVED FROM THE SERVER 
    if(status.title==null){ 
     $('#core').attr('class','play'); 
     $('#button').animate({marginLeft:'350px'},2000,'easeOutCubic',function(){ 
      $('#button').click(initialBinder()); 
     }); 
    }else{ 

    } 
} 
function initialBinder(){ 
    $('#button').unbind('click'); 
    $('#core').attr('class','load'); 
    $('#button').animate({marginLeft:'0px'},2000,'easeOutCubic',function(){ 
     $.ajax({ 
      url:'http://24.182.211.76/status.php', 
      crossDomain:true, 
      dataType:'jsonp' 
     }); 
    }); 
} 
$(document).ready(function(event){ 
    $('#button').click(initialBinder()); 
}); 

답변

10

변경 :

$('#button').click(initialBinder()); 

에 :

$('#button').click(initialBinder); 

는 전 실제로 전화 기능, 여기에 함수 포인터를 반환하지 않습니다 여기 내 코드입니다.

function foo(x, y) //I'm function foo, I add two numbers like a boss 
 
{ 
 
    return x + y; 
 
}; 
 

 
var x = foo(1,1); //x is 2 because we called the function 
 
var y = foo; //y is a reference to function foo, no parentheses 
 
var z = y(1,1); //z is 2 because we called foo through reference y 
 
document.write('x = ' + x + '<br />z = ' + z);

+0

나를 이길 : ( – Aaron

+0

일 했어! 결코 알지 못했다. 고마워! – nkcmr

+1

@ 테겔 릴 - 내가 열렬히 타이핑했다! :) –

2
$('#button').click(initialBinder); 

당신은 콜백과 같은 함수를 참조하지만, 함수를 호출하지 않은 :

아마 코드의 약간의 차이를 명확히 할 수 있습니다. 이처럼 바인드 문에서 괄호를 제거

0

봅니다 :

$(document).ready(function(event){ 
    $('#button').click(initialBinder); 
}); 

무엇이 일어나고있는 것은 함수가 아니라 인수로 전달되는 것이 아니라 실행지고 있다는 점이다.

관련 문제