我有一段代碼,它每隔十分之一秒向文檔添加一個div,將它隨機移動到頁面邊緣,然後將其刪除。目前幀率非常低,所以我想知道是否有辦法讓div在頁面邊緣離開時自動移除(左邊或頂部值超過100%或低於0%)自動從DOM中刪除元素
或是否有任何其他方式來達到幀速率...
下面的代碼:
function myFunction() {
//the following generates four random numbers between 100-400 and asigns 2 of them to be the top and left values
var RN=Math.floor(Math.random()*2);
var RN2=Math.floor(Math.random()*2);
var RNMB4=Math.random()*300+100;
var RNMB2=Math.random()*300+100;
var RNMB3=Math.abs(Math.random()*300) * -1;
var RNMB1=Math.abs(Math.random()*300) * -1;
var NMBRS=[RNMB1,RNMB2];
var NMBRS2=[RNMB3,RNMB4];
$("<div/>").appendTo('body').animate({
left: NMBRS[RN] + '%',
top: NMBRS[RN] + '%',
},
1000), function(){$(this).remove();});
}
setInterval(myFunction,100);
使用螢火蟲或鉻來分析代碼。不要猜測效率低下的地方。 – Kyle 2013-04-28 15:55:02