Javascript 如何在jquery中每隔5秒替换一个除法中的值?
我想每隔5秒用新值替换idJavascript 如何在jquery中每隔5秒替换一个除法中的值?,javascript,jquery,Javascript,Jquery,我想每隔5秒用新值替换idshow_num_val的div中的值。为此,我写了以下内容。 <div id="show_num_val"> 0 </div> 0 在脚本中: <script> $(document).ready(function() { for(i=0;i<20;i++){ showThis(i); } }); function showThis(x) { setTimeout(func
show_num_val
的div
中的值。为此,我写了以下内容。<div id="show_num_val"> 0 </div>
0
在脚本中:
<script>
$(document).ready(function() {
for(i=0;i<20;i++){
showThis(i);
}
});
function showThis(x) {
setTimeout(function() {
$('#show_num_val').html(x);
}, 5000);
}
</script>
$(文档).ready(函数(){
对于(i=0;i,如果希望重复执行,可以使用
$(document).ready(function() {
x = 1;
inverval = setInterval(function() {
$('#show_num_val').html(x++);
if(x == 21)
clearInterval(inverval);
}, 2000);
});
看到它工作了吗
$(文档).ready(函数(){
对于(i=1;iYou是受欢迎的,我已经更新了我的答案,在20次迭代后停止它。请解释一下所编写的代码。它是做什么的/返回什么的?这对所有人都有帮助。。。。
var x = 0;
T = setInterval(function() {
if (x == 20) clearInterval(T);
$('#show_num_val').html(x);
x++;
}, 5000);
<script>
$(document).ready(function() {
for(i=1; i<20; i++){
showThis(i, 5000 * i));
}
});
function showThis(x, y) {
setTimeout(function() {
$('#show_num_val').html(x);
}, y);
}
</script>