Prototype Javascript Framework – PeriodicalExecuter

이번에는 쉬어갈겸 짧은걸로 선택했습니다-_-a 이 읽기도 힘든 PeriodicalExecuter는 window객체의 Native 메서드인 clearInterval/setInterval 를 사용한 클래스 입니다.

콜백 함수를 받아 주기적으로 그 함수를 실행하게 됩니다. 내부적으로 실행 플래그를 받아 반복 실행을 중단 시킬 수도 있습니다.

PeriodicalExecuter 생성하기 ————————————————————————————–

PeriodicalExecuter의 생성자는 두가지 인자를 받습니다. 첫번째는 콜백 함수를 받고 두번째로는 몇초단위로 지속 실행할지 시간을 받습니다.

기본적으로 PeriodicalExecuter는 페이지가 unload될때까지 끝없이 콜백함수를 실행합니다. 또는 수동으로 종료시킬 수 있습니다.

예제)
[code]// 캠프파이어 스타일 – 무조건 실행하면 되는 경우
new PeriodicalExecuter(pollChatRoom, 3);


new PeriodicalExecuter(function(pe) {
  if (!confirm(‘Want me to annoy you again later?’))
    pe.stop();
}, 5);
// 확인창이 뜨게 되며 취소를 누를경우 실행 중단[/code]

PeriodicalExecuter Methods ————————————————————————————–

stop : PeriodicalExecuter 실행을 중단 시킵니다.
[code]stop()[/code]
PeriodicalExecuter는 한번 생성되면 끝없이 실행됩니다. 하지만 특정한 이유에 의해 중단이 될 필요가 있습니다.

예제)
[code]var gCallCount = 0;


new PeriodicalExecuter(function(pe) {
  if (++gCallCount > 3)
    pe.stop();
  else
    alert(gCallCount);
}, 1);
// 1, 2, 3을 출력하는 창이 뜬 후 PE 종료[/code]