Automatic countdown timer to configure database records

I have a small MVC website that is designed for hairdressing friends. On this page, I have a div that is used to display the number that is required to write the database. This number represents the current number of people who are waiting in line waiting for a haircut.

Currently, I have the opportunity to log in to the admin page and update this number using the form from "2" to "5", and then change "5" to "6" depending on the number of people sitting in this queue.

This is handmade, as it stands now. Code below:

===============================

Controller

[HttpPost] public ActionResult Update(Data data) { if (ModelState.IsValid) { data.ID = 1; //EF need to know which row to update in the database. db.Entry(data).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index", "Home"); } return View(data); } 

======================================

Model code

 { public class Data { public int ID { get; set; } public string Queue_Number { get; set; } } public class DataDBContext : DbContext { public DbSet<Data>Queue { get; set; } } } 

I would very much like that after you manually updated the queue number from the form on the "admin" page, I would like the automatic countdown to be 20 minutes (rough time required for cutting) and then the queue number is automatically set to one until it reaches "0".

eg. We have 5 people in the queue, after 20 minutes it is automatically configured for 4 people, and the web page will be automatically updated / updated, and then 2 more people go, so we manually configure it for 6 people in the queue, and the timer starts again, every 20 minutes the queue is set to -1 until it drops to "0". Once it reaches "0", it stays there until we manually add more people to the queue.

I'm afraid I have no idea how to start with such a request, or even if it is possible?

I would be very grateful for any help from experts who could "get" it for me. Any information that I did not provide, I will try to add - I understand that I am not the best, explaining myself: - (

+7
source share
4 answers

Did you consider Ajax? Do you save the last updated time when manually setting the flag? You can use an Ajax request to run at the same time using the jquery Set interval. which will run an ajax request every 2 minutes. Find the last time it was updated, if it took 20 minutes, and then delete one from the database, your return will be a new number, and jquery can update this number for you.

A fairly simple process actually, but it needs more details about the underlying data.

This is how I see how it works from your question

In the controller

 public ActionResult ajaxUpdate() { //open connection dbcontext db = new dbcontext(); db.Connection.Open(); // get the last updated record in the database. var entry = db.Entry.OrderByDecending(m=> m.LastUpdatedDate).FirstOrDefault(); //clean up db.Connection.Close(); db.Dispose(); //return -1 as error if(entry == null){ return Json(-1,JsonRequestBehavior.AllowGet); } // get current number of people in queue Int32 numberOfPeople = entry.QueueNumber; TimeSpan span = DateTime.Now.Subtract(entry.LastUpdatedDate); if(span.Minutes >= 20){ // if 20 mins have passed assume a person has been completed since manual update numberOfPeople--; } //this returns a number, alternatively you can return a Partial return Json(numberOfPeople, JsonRequestBehavior.AllowGet); } 

JQuery and Ajax

 $(document).ready(function () { // run function every x minutes setInterval(function () { UpdateQueue(); }, 100000); }); function UpdateQueue() { $.ajax({ cache: true, type: 'POST', url: "/ControllerName/ajaxUpdate", async: false, dataType: "json", success: function (result) { // on success result will be the number returned // -1 is error if (result == -1) { return; } // check the -- didn't return a negative if (result < 0) { result = 0; } //find your element in the HTML to update $('#NumberElement').text().replaceWith(result); } }); } 

You must ensure that you have included your jquery libraries before including this code, or you will not have jQuery.

+7
source

I have prepared a server-side solution with a small flow for you. I hope that I am right to block critical sections.

This has the advantage that the administrator of your application does not need to hang on the page to get the number of current clients being reset (for example, it should with ajax requests).

How it works

In the window "Number of client updates", a new counting stream is started (if necessary), which waits (sleeps) for a predetermined interval and then decreases the number.

 public class CustomerAdminService { // time in milliseconds it will take to decrease number of waiting customers const int sleepTime = 10000; // current number of customers (just for simplicity - you can have it in db or somewhere else) static int numberOfCustomers; static Thread updaterThread; // object lock static readonly object locker = new Object(); public int GetNumberOfCustomers() { return numberOfCustomers; } public void UpdateCustomers(int value) { lock (locker) { if (updaterThread == null) { //start new downcounting thread updaterThread = new Thread(new ThreadStart(UpdateWorker)); updaterThread.Start(); } SetNumberOfWaitingCustomers(value); } } private void SetNumberOfWaitingCustomers(int value) { numberOfCustomers = value; } // downcounting thread method private void UpdateWorker() { while (true) { // sleep for predefined time Thread.Sleep(sleepTime); lock (locker) { var number = GetNumberOfCustomers(); if (number <= 1) { // if number of currents customers is now zero - end the downcounting thread SetNumberOfWaitingCustomers(0); updaterThread = null; return; } SetNumberOfWaitingCustomers(number - 1); } } } } 

A comment. You can use jQuery to count the timer script. Featuring something like: you can be served in 40 minutes; -)

+4
source

Yes Ajax is the key. It can be used by your site for discreet communication with your server.

+1
source

An alternative approach would be to not update the account in the database, but simply use the query to determine the number of customers over a period of time. You can do this by changing the model so that instead of QueueNumber it uses the arrival time and QueueNumber controller so that it inserts a new data record.

 { public class Data { public int ID { get; set; } public DateTime Arrival_Time { get; set; } } public class DataDBContext : DbContext { public DbSet<Data> Queue { get; set; } } } 

Thus, like others, you can use AJAX to poll the number of people in the queue with a controller action that might look something like this:

 [HttpGet] public ActionResult NumberOfPeopleInQueue() { var result = db.NumberOfCustomersSince(DateTime.Now.AddMinutes(-20)); return Json(result); } 

The nice thing about this approach is that if the haircuts start taking longer (say 30 minutes), you can just change the request and the application continues to work.

+1
source

All Articles