English 中文(简体)
使用 seetTimout 进行信头旋转的麻烦
原标题:Trouble using setTimout for header rotation Rails 2.3.9

我有不同的页眉,我想根据每次页数加载时应该执行的计时器来绕过。 目前,我的工作只有一次,但在头头第一次改变之后就停止了。我不太擅长刺绣,如果有人能指示我正确方向,我会感激不尽。

下面是我目前掌握的情况。

在lib/Rotator.rb中:

class Rotator
 def self.header_rotator(number)
   partials = ["header", "header2", "header3", "header4", "header5"]
   random_header = partials[number]
 return random_header
 end
end

然后在申请的页眉部分.html.haml,我有:

var start_rotate = setTimeout(rotate, 5000);
     function rotate() {
      remote_function(:url => {:action})
      $("#header").replaceWith( #{escape_javascript( render :partial => "shared/#{Rotator.header_rotator(rand(5))}")} )
      start_rotate = setTimeout(rotate, 5000);
     }

正如我说过的,这工作很好,但只旋转一次,然后停下来。

任何帮助都将不胜感激!

最佳回答

这更不在于js, 更在于理解运行地点和时间。

如果您看到正在转换的 HTML, 将会变得更清楚。 拨打 escape_javascript , render 时, 正好发生一次: 当该字符串在主页负载输入时被内插时。 因此, 第二次( 以及第三次、 第四次、 第五次) 旋转时, 它会用完全相同的 HTML 替换信头 。

假设您正在使用jquery, 装载方法可以做到这一点 :

$( #header ).load( /some/server/path )

在回应这一要求时,不要忘记没有布局。

最后, 您确定要使用 Ajax 吗? 您可以在前面加载所有的页眉变体, 并使用 JavaScript 来反过来显示它们 。 (有多个 jquery 周期或 lidshiw 插件可以这样做 )

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.