English 中文(简体)
如何限制Rails应用程序中的鼠标点击率?
原标题:How do I limit mouse click rate in a Rails app?

我写了一个考勤卡应用程序,我有一个页面,人们可以在这里“打卡”进出。每个人都有一个按钮,可以通过Ajax回调来更改他们的状态。有几个人发现,连续点击一个人的按钮一百次很有趣,所以打卡/下班时间的报告页面变得非常愚蠢。

我想在打孔页面中添加一个简单的Javascript函数,将页面的“可点击性”限制为大约每5秒一次。我发现了几个其他threads这里讨论的是类似的内容,但我对JS的了解还不够,无法将这些建议移植到Rails如何进行Ajax调用中。

最佳回答

您不能检查记录中的updated_at字段吗?它不会限制“鼠标”点击率,但可以防止您的记录每分钟/小时更新次数超过x

例如,在您的模型中,您可以有以下内容:

class TimeRecord < ActiveRecord::Base
  validate :throttled_updates, on: :update

  private
  def throttled_updates
    errors.add(:updated_at, "must be more than 5 minutes ago") if self.updated_at < 5.minutes.ago
  end
end 
问题回答

嗯,有几种可能性;这取决于你想要多大的控制力。

First you could limit in the JS how often the request can be made. As mentioned earlier. Also, you could make it part of the model logic, not accepting updates within a certain time (also mentioned earlier)

我的建议是对每个用户的请求进行评级限制。有一个很好的机架中间件:http://datagraph.rubyforge.org/rack-throttle/。如果需要,你可以添加定制功能,但我认为这是一个良好的开端。

http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to

选项:禁用

您可以在onclick事件之后禁用该按钮。用户必须重新加载页面才能重新单击。否则,您将需要一些JS在超时后重新启用。

# assumes you use jQuery
link_to  Toggle , toggle_path, :remote => true, :onclick =>  
    var e = $(this);
    if(e.data("busy") == "yes"){
        return false;
    } else {
        e.data("busy", "yes");
        setTimeout(function(){ 
            e.data("busy", "no");
        }, 5000);
    }
   .gsub("
",   )

检查http://jsfiddle.net/u42gQ/1/





相关问题
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.

热门标签