JavaScript“...”等待点以间隔循环

发布于 2024-09-14 07:20:40 字数 278 浏览 4 评论 0原文

我不想使用动画 gif,而是想在跨度中使用文本:

  1. 等待
  2. 等待。
  3. 等待..
  4. 等待...

我想循环遍历每个,然后返回到无限循环中的第一个。我还想控制间隔以加快或减慢速度。我希望我的 HTML 是:

<p>Waiting<span id="dots"></span></p>

并且简单地循环跨度innerHTML。谢谢!

Instead of using an animated gif, I would like to instead have text in a span:

  1. Waiting
  2. Waiting.
  3. Waiting..
  4. Waiting...

I would like to loop through each and then return to the first in an infinite loop. I would also like to control the interval to speed up or slow it down. I'd like to have my HTML to be:

<p>Waiting<span id="dots"></span></p>

And simply cycle the span innerHTML. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叫嚣ゝ 2024-09-21 07:20:40

这可能会让你朝着正确的方向前进。我认为代码应该能够自我解释。

function iterateDots(){
    var el = document.getElementById("dots");
    var dotsStr = el.innerHTML;
    var dotsLen = dotsStr.length;

    var maxDots = 3;
    el.innerHTML = (dotsLen < maxDots ? dotsStr + '.' : '');
}

function startLoading(){
    var intervalMs = 300;

    var interval = setInterval("iterateDots()", intervalMs);
}    

This would probably set you in the right direction. I think the code should explain itself.

function iterateDots(){
    var el = document.getElementById("dots");
    var dotsStr = el.innerHTML;
    var dotsLen = dotsStr.length;

    var maxDots = 3;
    el.innerHTML = (dotsLen < maxDots ? dotsStr + '.' : '');
}

function startLoading(){
    var intervalMs = 300;

    var interval = setInterval("iterateDots()", intervalMs);
}    
作业与我同在 2024-09-21 07:20:40

dots = document.getElementById('dots'); 将获取您想要在其中放置点的 DOM 元素,并且该元素的 innerHTML 属性将允许您操作内容在里面。

至于在特定时间间隔更改跨度的内容,请查看 setInterval( )setTimeout()。您必须选择最适合您需求的一种。

我不会发布实际的解决方案,因为您尚未在这方面真正付出任何努力,但这应该为您指明正确的方向。

dots = document.getElementById('dots'); will get the DOM element you want to put dots in, and the innerHTML attribute of that element will let you manipulate the content inside it.

As for changing the content of the span on a specific interval, have a look at setInterval() and setTimeout(). You'll have to pick the one that best suits your needs.

I'm not going to post an actual solution since you haven't really showed any effort on this one, but this should point you in the right direction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文