改变span的内容然后等待
我的问题:我有一个包含数组的 JSON 数组,我将其渲染到 body 标记中 - 使其看起来像这样:
<body data-flashes="[["message","Welcome"],["error","This is working!"],["",""]]">
我想要执行以下操作:
- 转到外部数组中的每个元素,
- 内部数组中的第一个元素将成为Span 的类名
- 内部数组中的第二个元素将成为 Span 中的文本 对 Span
- 进行动画处理
- 等待 10 秒
- 重新开始
(它始终是相同的 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:
- go each element in the outer array
- the first element in the inner array will become the classname of a span
- the second element in the inner array will become the text in a span
- animate the span
- wait 10 seconds
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
setTimeout() 是所有这一切的解决方案。
它需要两个参数,第一个是要执行的字符串,第二个是从现在开始的延迟(以毫秒为单位)。
为了缓解这个问题,我将代码块包装在一个函数中,然后使用 setTimeout() 调用该函数。例如:
意味着 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:
Would mean that do_stuff() is going to be called in 10seconds time.
I hope that helps.