2010-12-20 3 views
1

html로, 버튼을 만듭니다. 버튼이 초당 한 번 클릭해야합니다. 내가 할 수있는 더 빨리 또는 더 나은 솔루션이,자바 스크립트, 빨리 버튼을 클릭하십시오 ~

inputTooFast = function() 
{ 
    var curDate = new Date(); 
    if(curDate.getHours() == this.lastinputtime[0]) 
     if(curDate.getMinutes() == this.lastinputtime[1]) 
      if(curDate.getSeconds() == this.lastinputtime[2]) 
       if((curDate.getMilliseconds() - this.lastinputtime[2]) < 999) 
       { 
        this.lastinputtime[0] = curDate.getHours(); 
        this.lastinputtime[1] = curDate.getMinutes(); 
        this.lastinputtime[2] = curDate.getSeconds(); 
        this.lastinputtime[3] = curDate.getMilliseconds(); 
        return true; 
       } 
    this.lastinputtime[0] = curDate.getHours(); 
    this.lastinputtime[1] = curDate.getMinutes(); 
    this.lastinputtime[2] = curDate.getSeconds(); 
    this.lastinputtime[3] = curDate.getMilliseconds(); 
    return false; 
} 

및 샘플 사용이 여기에 너무

function sendMessage() 
{ 
    if(theObj.inputTooFast()==true) 
     return; 

... 
    some valid code here 
... 

} 

그래서 내 코드는 여기에 ~

을 빠른 버튼 클릭을 허용하지 않으려는 이? jquery를 사용하고 있습니다.

+0

일반적으로 채팅 보내기 버튼은이 기능을 사용할 것입니다 ~ –

+1

~, 나는 단지 대답을 수락하는 방법을 몰랐습니다. 귀하의 질문에, 나는 그것을 발견 ~ 감사 ~ –

답변

4

사용자가 단추를 클릭 할 수 없다는 의견을 보내지 않으려 고하는 것 같습니다. 당신은 사용자의 피드백 (일반적으로 좋은 생각)을 부여하려면,

sendMsg.blocked = 0; 
function sendMsg() { 
    if (sendMsg.blocked == 0) { 
     ++sendMsg.blocked; 
     setTimeout(removeSendMsgBlock, 1000); // If you want one second 

     // ...other processing 
    } 
} 
function removeSendMsgBlock() { 
    --sendMsg.blocked; 
} 

(이 같은 버튼을 비활성화 : 그렇다면 (아래를 참조하지 않은 경우), 아마이 (live example를) 같은 것을 할 거라고 live example) :

btnSendMsg = /* ...get the button element... */; 
function sendMsg() { 
    btnSendMsg.disabled = true; 
    setTimeout(removeSendMsgBlock, 1000); // If you want one second 

    // ...other processing 
    display("Sending message at " + new Date()); 
} 
function removeSendMsgBlock() { 
    btnSendMsg.disabled = false; 
} 

업데이트 : 사실, 여분의 심볼에 대한 필요가 없습니다, 단지 너무 sendMsg 기능 개체에서 제거 함수 참조를 넣어 :

재 16,

예 1

sendMsg.blocked = 0; 
function sendMsg() { 
    if (sendMsg.blocked == 0) { 
     ++sendMsg.blocked; 
     setTimeout(sendMsg.removeBlock, 1000); // If you want one second 

     // ...other processing 
    } 
} 
sendMsg.removeBlock = function() { 
    --sendMsg.blocked; 
}; 

재 예 2 :

btnSendMsg = /* ...get the button element... */; 
function sendMsg() { 
    btnSendMsg.disabled = true; 
    setTimeout(sendMsg.removeBlock, 1000); // If you want one second 

    // ...other processing 
    display("Sending message at " + new Date()); 
} 
sendMsg.removeBlock = function() { 
    btnSendMsg.disabled = false; 
}; 
+0

와우, 좋은 직장 !! 고마워 ~ –

2

배열에서 해당 필드를 저장할 필요 없음. 당신은 Date로를 저장하거나 할 수 있습니다 만 나중에 숫자 비교를 위해 그것을 사용하고 있기 때문에 내가 여기서 뭘 선택한 number, 같은 :

inputTooFast = function() { 
    return (new Date()).getTime() - this.lastInputTime < 1000; 
} 

참고이 false를 반환하는 경우, 당신은 계속, lastInputTime을 저장해야합니다.

관련 문제