js settimeout定时 for循环问题 闭包先做个笔记 留待研究 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script type="text/javascript" src="jquery.1.7.2.min.js" ></script> </head> <body> <div>0</div> <script> //1 /* for(var i=0;i<5;i++){ setTimeout(function(){ alert(i); },i*100); } */ //2 /* var createFunction=function(i){ return function(){ alert(i); } } for(var i=0;i<5;i++){ setTimeout(createFunction(i),i*100); } */ $(function(){ /* for(var i=0;i<5;i++){ setTimeout(alert(i),i*1000) } */ var t=function(i){ return function(){ $(‘div‘).html(i) } } for(var i=0;i<5;i++){ setTimeout(t(i),i*1000) } }) //3 </script> </body> </html>
本文出自 “甲午笔记” 博客,请务必保留此出处http://jiawu.blog.51cto.com/9349234/1567653
原文地址:http://jiawu.blog.51cto.com/9349234/1567653