Web/JavaScript

[javascript] delay하고 처리하기

opid 2013. 8. 17. 11:01

어느 정도 시간을 준 뒤에 자바스크립트를 처리하려 할때 사용할 수 있다.


setTimeout( function, mile second );

ex) setTimeout("helloworld()", 1000);    // 1000 mile second == 1 second


일정시간 딜레이만 주려면 아래와 같은 함수를 만들어 사용하면 된다.


function delay(gap){ /* gap is in millisecs */ 

  var then,now; 

  then=new Date().getTime(); 

  now=then; 

  while((now-then)<gap){ 

    now=new Date().getTime();  // 현재시간을 읽어 함수를 불러들인 시간과의 차를 이용하여 처리 

  }