gpt4 book ai didi

Javascript createElement 与 setInterval

转载 作者:行者123 更新时间:2023-11-28 04:55:40 24 4
gpt4 key购买 nike

所以我在尝试完成一个小任务时遇到了一些问题,我有点卡在了这一点上。

我正在制作一个应用程序,当我单击一个按钮时,该应用程序会在 setInterval 循环中使用 createElement 创建 div,在 1 秒(约 50 毫秒)内创建多达 20 个 div。我也可以停止间隔,然后重新开始。这将生成一个 20px X 20px 的红色小框。这是代码:

<script>    
var box;
var counter = 0;

function makeNew() {
document.body.appendChild(
document.createElement('box'));
counter++;
document.getElementById('boks').innerHTML = counter;
}

function startInterval() {
box = setInterval(function () { lagNy() }, 50);

}
function stoppInterval() {
clearInterval(box);
}
</script>

<body>
<input type="button" id="start" value="Generer" onclick="startInterval();" />
<input type="button" id="stopp" value="Stopp" onclick="stoppInterval();" />
</body>

我真正需要帮助的是,我想在这些 div 中打印数字,并且它随着每个创建的 div(框)而递增。像这样:box1(1)、box2(2)、box3(3) 等....

对此有任何想法、提示或帮助吗?

非常感谢您的帮助!

最佳答案

尝试 this demo .

HTML

<input type="button" id="start" value="Generer" onclick="startInterval();" />
<input type="button" id="stopp" value="Stopp" onclick="stoppInterval();" />

<p id="boks"></p>

JS

var timer,
counter = 0;

function makeNew() {
document.body.appendChild( document.createElement('div') );
counter++;
document.getElementById('boks').innerHTML += 'box('+counter+'),';
}

function startInterval() {
timer = setInterval(makeNew, 50);

}

function stoppInterval() {
clearInterval(timer);
}

更新:查看您的问题可能是您正在尝试获得此 http://jsfiddle.net/aamir/84HgL/2/

关于Javascript createElement 与 setInterval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23675029/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com