这是我写的排序代码(不同高度的柱形条)的一部分,就是交换两个数字的位置,排序正常。
Array.prototype.swap=function(a, b)
{
var tmp=this[a];
this[a]=this[b];
this[b]=tmp;
document.getElementById("div"+a).style.width = this[a]+'px';
document.getElementById("div"+b).style.width = this[b]+'px';
}
我想交换位置有一定的延迟,就改成了如下代码:
Array.prototype.swap=function(a, b)
{
setTimeout(function(){
var tmp=this[a];
this[a]=this[b];
this[b]=tmp;
document.getElementById("div"+a).style.width = this[a]+'px';
document.getElementById("div"+b).style.width = this[b]+'px';
},100);
}
但是再次执行排序确根本不执行,为什么啊?thanks!
Array.prototype.swap=function(a, b)
{
var tmp=this[a];
this[a]=this[b];
this[b]=tmp;
document.getElementById("div"+a).style.width = this[a]+'px';
document.getElementById("div"+b).style.width = this[b]+'px';
}
我想交换位置有一定的延迟,就改成了如下代码:
Array.prototype.swap=function(a, b)
{
setTimeout(function(){
var tmp=this[a];
this[a]=this[b];
this[b]=tmp;
document.getElementById("div"+a).style.width = this[a]+'px';
document.getElementById("div"+b).style.width = this[b]+'px';
},100);
}
但是再次执行排序确根本不执行,为什么啊?thanks!
你把this[a] this[b]都先放到外面来 。
Array.prototype.swap=function(a, b)
{ var that = this;
setTimeout(function(){
var tmp=that[a];
that[a]=that[b];
that[b]=tmp;
document.getElementById("div"+a).style.width = that[a]+'px';
document.getElementById("div"+b).style.width = that[b]+'px';
},100);
}
{
setTimeout(function(){
var tmp=this[a];
this[a]=this[b];
this[b]=tmp;
document.getElementById("div"+a).style.width = this[a]+'px';
document.getElementById("div"+b).style.width = this[b]+'px';
},100).call(this);
}这样也不行?