87,910
社区成员
发帖
与我相关
我的任务
分享
var p = new Promise(function(resolve, rejectA){
setTimeout(function(){
console.log(2);
resolve();
}, 1000);
console.log(1);
});
p.then(function(){ // 等待前一个resolve调用后才执行
console.log(3);
});
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>递归案例</title>
<style>
.hnt{
width:300px;
height:120px;
background:#9FF;
border:#30F thin dotted;
text-align:center;
float:left;
}
</style>
<script type="text/javascript">
var level=5;//层数
var delay=500;//延迟
var sdelay = 0;
var data=["1","222","33333","4444444","555555555"];
var a=new Array(5),b=new Array(5),c=new Array(5);
function init()
{
for(var i=0;i<data.length;i++)//小数在上,大数在下
{
a[level-1-i]=data[i];
b[i]="";
c[i]="";
}
viewHnt();
hnt(level,a,b,c);
}
function hnt(n,A,B,C)//移动n从哪里,经过哪里,到达哪里.n为柱子数组的有效长度
{
if(n==1){
move(A,C);
}
else{
hnt(n-1,A,C,B);//把n-1个盘子先移动到passP
move(A,C);
hnt(n-1,B,A,C);
}
}
function move(start,end)
{
var h1=getHeight(start);
var h2=getHeight(end);
end[h2]=start[h1-1];
start[h1-1]="";
viewHnt();
}
function getHeight(sz)
{
var h=0;
for(var i=0;i<level;i++)
{
if(sz[i]!="")h++;
}
return h;
}
function viewHnt()
{
var diva=document.getElementById("A");
var divb=document.getElementById("B");
var divc=document.getElementById("C");
var bottom="===========";
var stra="",strb="",strc="";
for(var i=0;i<level;i++)
{
var v=a[i]==""?"|":a[i];
stra=v+"<br/>"+stra;
var v1=b[i]==""?"|":b[i];
strb=v1+"<br/>"+strb;
var v2=c[i]==""?"|":c[i];
strc=v2+"<br/>"+strc;
}
setTimeout(function(){
diva.innerHTML=stra+bottom;
divb.innerHTML=strb+bottom;
divc.innerHTML=strc+bottom;
}, sdelay);
sdelay+=delay;
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<p> <input type="button" name="button" id="button" value="开始汉诺塔" onclick="init()"/>
</p>
<div id="A" class="hnt"></div>
<div id="B" class="hnt"></div>
<div id="C" class="hnt"></div>
</form>
</body>
</html>