2011-12-21 2 views
2

저는 자바 초보자이며 필자가 작성한이 코드에 문제가 있습니다. 그것은 내 웹 사이트를위한 디지털 시계를 만드는 것으로되어 있습니다. CPGS 내 기능에서/변수 이름 그건 왜 당신이 궁금해하는 경우간단한 시간 시계가 오류없이 작동하지 않습니다.

그 때문에 내 웹 사이트 이름 : 또한

에 대한 약어, 나는 방화범이 끌려 내 JSLint에서 NO 콘솔 오류를 받고 없습니다하고 나의 코드 것을 확인 배심원이다.

여기에 코드입니다 :

(function() { 
    function CpgsClock() { 
     this.cpgsTime = new Date(); 
     this.cpgsHour = this.cpgsTime.getHours(); 
     this.cpgsMin = this.cpgsTime.getMinutes(); 
     this.cpgsDay = this.cpgsTime.getDay(); 
     this.cpgsPeriod = ""; 
    } 

    CpgsClock.prototype.checker = function() { 
     if (this.cpgsHour === 0) { 
      this.cpgsPeriod = " AM"; 
      this.cpgsHour = 12; 
     } else if (this.cpgsHour <= 11) { 
      this.cpgsPeriod = " AM"; 
     } else if (this.cpgsHour === 12) { 
      this.cpgsPeriod = " PM"; 
     } else if (this.cpgsHour <= 13) { 
      this.cpgsPeriod = " PM"; 
      this.cpgsHour -= 12; 
     } 
    }; 

    CpgsClock.prototype.setClock = function() { 
     document.getElementById('cpgstime').innerHTML = "" + this.cpgsHour + ":" + this.cpgsMin + this.cpgsPeriod + ""; 
    }; 

    var cpgsclock = new CpgsClock(); 
    setInterval(function() { 
     cpgsclock.setClock(); 
     cpgsclock.checker(); 
    }, 1000); 

})(); 

그래서 setClock 방법은 잘 작동합니다. 하지만 검사기는 아무 것도하지 않습니다. 보시다시피 시간을 확인하고 그에 따라 AM 및 PM으로 설정합니다. 그것은 나를 위해하지 않습니다.

도움이 될 것입니다. 당신의 setInterval에서

답변

0

당신은 당신의 setInterval을에서 시계를 업데이트하지 않는 ...

CpgsClock.prototype.update = function() { 
    this.cpgsTime = new Date(); 
    this.cpgsHour = this.cpgsTime.getHours(); 
    this.cpgsMin = this.cpgsTime.getMinutes(); 
    this.cpgsDay = this.cpgsTime.getDay(); 
    this.cpgsPeriod = ""; 
}; 

그리고 :

setInterval(function() { 
    cpgsclock.update(); 
    cpgsclock.checker(); 
    cpgsclock.setClock(); 
}, 1000); 

jsFiddle : http://jsfiddle.net/qmSua/1/

+0

아, 감사합니다! 내가 지금 그것을 어떻게 원 하든지 단지 작동한다 :) – Dropped43

관련 문제