【发布时间】:2023-04-06 23:36:01
【问题描述】:
在我的程序中,我正在使用 JavaScript 制作 div 框,制作 50X50 网格似乎需要相当多的时间。制作 20x20 网格甚至需要一些时间。我已经查找了如何使我的代码更快,但没有任何建议产生微不足道的差异。
这是一个奥丁项目
https://jsfiddle.net/Mulk/yc5rsf1m/#&togetherjs=dVAh1FK7On
$(document).ready(function(){
// Defalut Grid is 16x 16 Grid
CreateBox(16);
CallMeMaybe();
$("#gridbtn").click(function(){
$(".odd").remove();
var numbox =parseInt(prompt("How many boxes would like?"));
CreateBox(numbox);
});
function CreateBox(a) {
var wh = (500/a), i , j ;
for(i=0;i<a;i++){
for(j=0;j<a;j++){
$div = $('<div/>').appendTo('#container').addClass(".odd").attr('class','odd').width(wh).height(wh);
CallMeMaybe();
}}
};
// Play with me
function CallMeMaybe(a){
$(".odd").hover(function(){
var r = Math.floor(Math.random() * (256 - 0) + 0);
var g = Math.floor(Math.random() * (256 - 0) + 0);
var b = Math.floor(Math.random() * (256 - 0) + 0);
var color = "rgb("+r+","+g+","+b+")"
$(this).css("background-color", color);
});
};
// Play with me
});
【问题讨论】:
-
我投票决定将此问题作为题外话结束,因为它最好放在codereview.stackexchange.com。
标签: javascript jquery html performance