一个简单的定时器问题(如何执行N次数的函数,每次执行都延迟指定时间)?
RT。 
 我想利用js的settimeout来完成这样一个功能: 
 window.onload=sett; 
 var   test   =   { 
 	fetch_content   :   function   (){ 
 	                           var   o   =   document.getElementById( 'ajax-response '); 
 		o.style.backgroundColor   =    '#fff333 '; 
 		o.innerHTML   =   Math.random(); 
 	} 
 } 
 function   sett() 
 { 
 for(i=0;i <5;i++){ 
 	setTimeout( "test.fetch_content() ",500); 
 	} 
 } 
 即执行5次test.fetch_content(),每次都延迟500ms。可是经过实验发现无法实现这个要求,请高手指正啊!!!!
------解决方案--------------------实际是执行了,for循环过快,所以你看到的效果应该只是最后一个的结果。 
 可以这样: 
 function fetchN(n) 
 { 
   if (n >  0) 
   { 
     n--; 
     setTimeout( "test.fetch_content() ", 500); 
     setTimeout( "fetchN( "+n+ ") ",500); 
   } 
 } 
 function sett() 
 { 
   setTimeout( "fetchN(5) ",500); 
 }
------解决方案--------------------window.onload=sett; 
 window.tt=0; 
 var test = { 
 	fetch_content : function (){ 
 	         var o = document.getElementById( 'ajax-response '); 
 		o.style.backgroundColor =  '#fff333 '; 
 		o.innerHTML = Math.random(); 
 	} 
 } 
 function sett(){ 
    test.fetch_content(); 
    if(window.tt <5){ 
       window.tt++; 
       setTimeout( "sett() ",500); 
    } 
 }