改变span的内容然后等待

发布于 2024-12-28 00:29:47 字数 2023 浏览 4 评论 0原文

我的问题:我有一个包含数组的 JSON 数组,我将其渲染到 body 标记中 - 使其看起来像这样:

<body data-flashes="[["message","Welcome"],["error","This is working!"],["",""]]">

我想要执行以下操作:

  1. 转到外部数组中的每个元素,
  2. 内部数组中的第一个元素将成为Span 的类名
  3. 内部数组中的第二个元素将成为 Span 中的文本 对 Span
  4. 进行动画处理
  5. 等待 10 秒
  6. 重新开始

(它始终是相同的 Span) 我的尝试:

window.pause = ( ms ) -> (
    date = new Date()
    currentDate = null

    loop
        currentDate = new Date()
        break unless currentDate-date < ms
)
window.showFlashes = () -> (
    $.each( window.$('body').data('flashes'), () -> (
            window.$('#flash_container').attr( 'class', this[0] )
            window.$('#flash_container').text( this[1] )
            window.$('#flash_container').effect( 'pulsate' )

            alert this

            pause( 10000 )
        )
    )
)

这是coffescript,rails将其渲染到以下javascript文件中:

(function() {

  window.pause = function(ms) {
    var currentDate, date, _results;
    date = new Date();
    currentDate = null;
    _results = [];
    while (true) {
      currentDate = new Date();
      if (!(currentDate - date < ms)) {
        break;
      } else {
        _results.push(void 0);
      }
    }
    return _results;
  };

  window.showFlashes = function() {
    return $.each(window.$('body').data('flashes'), function() {
      window.$('#flash_container').attr('class', this[0]);
      window.$('#flash_container').text(this[1]);
      window.$('#flash_container').effect('pulsate');
      alert(this);
      return pause(10000);
    });
  };

  window.exit_after = function(ms) {
    var _results;
    setTimeout("return 0", ms);
    _results = [];
    while (true) {
      _results.push(ms = 1);
    }
    return _results;
  };

}).call(this);

现在是“真正的”问题:只要我在代码中有alert this,它就可以正常工作(除了动画不是)在职的)。但是当我删除它时,代码停止工作。

我的第二个问题是:有没有办法绕过 pause 函数?我宁愿使用 setTimeout(),但我不知道如何...

My problem: I have an JSON Array containing arrays, which i render into the body tag - so that it looks like this:

<body data-flashes="[["message","Welcome"],["error","This is working!"],["",""]]">

i want to do the following:

  1. go each element in the outer array
  2. the first element in the inner array will become the classname of a span
  3. the second element in the inner array will become the text in a span
  4. animate the span
  5. wait 10 seconds
  6. start over again

( its always the same span)
my try:

window.pause = ( ms ) -> (
    date = new Date()
    currentDate = null

    loop
        currentDate = new Date()
        break unless currentDate-date < ms
)
window.showFlashes = () -> (
    $.each( window.$('body').data('flashes'), () -> (
            window.$('#flash_container').attr( 'class', this[0] )
            window.$('#flash_container').text( this[1] )
            window.$('#flash_container').effect( 'pulsate' )

            alert this

            pause( 10000 )
        )
    )
)

this is coffeescript which rails renders into following javascript-file:

(function() {

  window.pause = function(ms) {
    var currentDate, date, _results;
    date = new Date();
    currentDate = null;
    _results = [];
    while (true) {
      currentDate = new Date();
      if (!(currentDate - date < ms)) {
        break;
      } else {
        _results.push(void 0);
      }
    }
    return _results;
  };

  window.showFlashes = function() {
    return $.each(window.$('body').data('flashes'), function() {
      window.$('#flash_container').attr('class', this[0]);
      window.$('#flash_container').text(this[1]);
      window.$('#flash_container').effect('pulsate');
      alert(this);
      return pause(10000);
    });
  };

  window.exit_after = function(ms) {
    var _results;
    setTimeout("return 0", ms);
    _results = [];
    while (true) {
      _results.push(ms = 1);
    }
    return _results;
  };

}).call(this);

now the "real" problem: as long as i have the alert this in the code it works fine (besides that the animation is not working). but when i remove it, the code stops working.

and my second problem is: is there a way to get around the pause function? i would rather like to use setTimeout(), but i dont know how to...

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

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

发布评论

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

评论(1

慢慢从新开始 2025-01-04 00:29:47

setTimeout() 是所有这一切的解决方案。

它需要两个参数,第一个是要执行的字符串,第二个是从现在开始的延迟(以毫秒为单位)。

为了缓解这个问题,我将代码块包装在一个函数中,然后使用 setTimeout() 调用该函数。例如:

function do_stuff()
{
...
}

setTimeout("do_stuff()",10000);

意味着 do_stuff() 将在 10 秒内被调用。

我希望这有帮助。

setTimeout() is the solution to all of this.

It takes two arguments, the first being a string to be executed and the second to be the delay in milliseconds from now.

To ease this, I would wrap your block of code in a function and then call this function using setTimeout(). For instance:

function do_stuff()
{
...
}

setTimeout("do_stuff()",10000);

Would mean that do_stuff() is going to be called in 10seconds time.

I hope that helps.

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