Programming/AJAX

prototype 사용한 테스트 Ajax 샘플

달나라민군 2011. 3. 2. 16:58

prototype.js 1.7 버전

Ajax.PeriodicalUpdater 클래스는 주기적으로 Ajax.Updater를 생성하도록 하는데 사용됩니다.
이 방법을 통해서 특정 시간 주기마다 페이지의 어떤 요소를 업데이트 하도록 할 수 있으며,
이 기능은 주식 정보나 RSS 리더 등 사용자에게 항상 최신의 정보를 보여 주어야 하는 어플리케이션에 적합합니다.
또한  Ajax.Updater의 옵션에 2개의 추가적인 파라미터를 더 가지며, frenquency 와 decat 가 그것입니다.
frenquency 는 초 단위의 값으로 몇 초마다 업데이트를 진행할지를 결정하며, 기본값은 2초입니다.
decay 는 서버가 변화 없는 데이터를 보낼 때, 주기적으로 로드하는 시간을 frequency에서 몇 배 늘릴 것인가를
지정하는 값이ㅣ며, 기본값이 1이므로 데이터가 변화되지 않더라도 로드하는 시간은 변화가 없습니다.

rand.html

<!DOCTYPE html>
<html>
<head>

 <script type="text/javascript" src="prototype.js"></script>
 <script type="text/javascript">
  function checkprice(){
   var myAjax = new Ajax.PeriodicalUpdater('price','rand.php',{method:'post',frequency:3.0, decay:1});
  }
 </script>
</head>
<body onload="checkprice()">
<h2>주가 알리미</h2>
<p>현재 주가지수:</p>
<div id="price"></div>
</body>
</html>

rand.php

<?php
 srand((double) microtime()*1000000);
 $price = 50+rand(0,5000)/100;
 echo "$price";
?>