如何在HTML中的每个项目之间打印带有延迟的列表 [英] How to print a list with a delay between each item in HTML

查看:56
本文介绍了如何在HTML中的每个项目之间打印带有延迟的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<html>
    <body>
        //Id for each item
        <p id=1></p>
        <p id=2></p>
        <p id=3></p>
        <script>
            for(i = 0; i < 3; i++) {
                window.setTimeout(press, 1000);
                //Should use For loop variable to fetch and print specific element's Id
                function press() {
                    document.getElementById(i).innerHTML = i;   
                }
            }
        </script>
    </body>
</html>

我对所有这些都有些不了解.这些命令大多数是我从w3schools获得的,我只是想将所有内容一点一点地拼凑起来.

I'm a bit of a noob to all of this. Most of these commands I got from w3schools and I'm just trying to piece everything together bit by bit.

推荐答案

您可以将参数传递给超时函数,因此我们可以使用该参数来显示第一个值,然后递增该值,如果该值再次开始超时是< = 3 :

You can pass an argument through to the timeout function, so we can use that to display the first value, then increment it and start the timeout again if the value is <= 3:

window.setTimeout(press, 1000, 1);
//Should use For loop variable to fetch and print specific element's Id
function press(j) {
  document.getElementById(j).innerHTML = j;
  if (++j <= 3) window.setTimeout(press, 1000, j);
}

每个项目的

//Id for each item
<p id=1></p>
<p id=2></p>
<p id=3></p>

这篇关于如何在HTML中的每个项目之间打印带有延迟的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆