0

I am trying to implement something in ASP.NET MVC where I can make each user perform an action once every n minutes.

I have come across controls such as Timer but, not quite sure what to use. I would like it so that when the user performs the action, a timer begins to count down from, for example, 3 minutes to 0:00. Once the 3 minutes have lapsed, the user will be able to perform the action again.

Any ideas how I can achieve this? Would I need a Timer Control?

0

4 Answers 4

1

There is a perfect solution here: Best way to implement request throttling in ASP.NET MVC? created by SO team.

Basically the idea is to store page hits for each user in the cache and then respond to a request based on your logic.

Sign up to request clarification or add additional context in comments.

1 Comment

Sounds like a sledge hammer for cracking a nut for what I need.
1

You could use jquery-timer http://code.google.com/p/jquery-timer/

Basic use would be disable button, invoke timer and then re-enable button when it completes.

However, if you want to post back in the interim this would not work.

Comments

1

The easiest way to do this would be to save the time of the last action in the session and on subsequent requests check whether it has been more than 3 minutes. For example:

public ActionResult DoSomething(){
     if (Session["LastAction"] == null || (DateTime.Now - (DateTime)(Session["LastAction"])).Minutes > 3){
          // do action
          Session["LastAction"] = DateTime.Now;
          return View("OK. action executed");
     }
     else{
          return View("Please wait");
     }
}

17 Comments

This seems feasible and makes clear sense. I shall try this out!
What if the user deletes cookies? Will this method still work?
No, it won't. If you want it to be related to the user you will always have a possibility of circumventing it. If he opens another browser it won't work either.
@Subby no it won't. Unless you store your session in the URL.
Would it therefore be wise to store the time against the User in the database?
|
1

The most elegant way to achieve this is implementing a job scheduler like Quartz.NET and defining proper job (what has to be done) and proper trigger (when/how often has to be done). With Quartz you can also implement much more complex conditions ("do it every Monday" etc).

Hope it will help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.