php - Javascript multiple countdown timer for different instants with dynamic arguments by ajax call
Get the solution ↓↓↓I searched everywhere on stackoverflow and google it but still I did not found any solution, so I am posting it here. Please help me to resolve it.
1.Normal e commerce site with add products to cart without refreshing the page and has to start countdown timer for each products seperately(It's a unique product and has to buy it within the time, jiust like auction).
2.When the user clicks on checkout has to freeze the timer and if he comes back to shop again has to start the timer where it has freeze.
So here is my code for onclick add to cart function
<input type="button" onClick = "cartAction('add','productId','Timer')" />
In script
function cartAction(action, id, time) {
var queryString = "";
if (action != "") {
switch (action) {
case "add":
queryString = 'action=' + action + '&id=' + id + '&quantity=1' + '&time=' + time;
break;
case "remove":
queryString = 'action=' + action + '&id=' + id;
break;
case "empty":
queryString = 'action=' + action;
break;
}
}
jQuery.ajax({
url: "ajax_action.php",
data: queryString,
type: "POST",
success: function (data) {
$("#cart-item").html(data);
$("#amount-top").html($("#total").val());
$("#item-total").html($("#carttotal").val());
if (action != "") {
switch (action) {
case "add":
$("#add_" + id).hide();
$("#added_" + id).show();
break;
case "remove":
$("#add_" + id).show();
$("#added_" + id).hide();
break;
case "empty":
$("#amount-top").html(0);
$("#item-total").html(0);
break;
}
}
},
error: function () {
}
});
}
And finally in ajax_action call is
How to freeze timer when checkout clicked and how to release the timer when user wants to add some more products to cart..
Thanks in advance.
Answer
Solution:
Clientside you can work with timers to show like a clock countdown. But I wouldn't save those timers if I were you, since it'll become a mess to keep everythign synced up every time the user closes browser / opens a new tab / any other action that stops setTimeout.
So what about using the timers clientside for showing the countdown clock, but just have a 'added to shop' timestamp for each product and a 'expires' timestamp as well. Then you won't have to mess with syncing timers when the user returns to the page, just do expiration - current to get the time remaining and set a new timer for the clock clientside instead of trying to resync an old timer.
Serverside just only add products to the cart if the current time is smaller than the expiration.
Answer
Solution:
You can start the timer server side. On clicking Add to cart button, send a ajax request to server to start a timer. Server will log the current time as start time and a end time(say 10 mins after the current time) in a temp DB table. This temp table will store all valid user timers.
The start ajax request will receive end time as response so that you know when to end the timer on client side. when the user reaches checkout page, send one more request to the server to check server side timer. The server will check in the temp table if the current time is less than the end time. If yes, it will set the freezetime as the (current time + allowed checkout time) in the temp table and return the freezetime like before else it will return timeout. This will you will know if the user has manipulated client side timer. Also closing the browser issue will be resolved with server side timer as well because its not affected with browser closing.
So next time when the user visits and you make the first call to start/check the timer, you will check if the current time is greater that the freeze time. If yes, then subtract freeze time from end time and check if the current time is less than the new end time then return the new end time and update temp table with new end time. IF current time is greater than the new end time then return timeout and delete the entry/row from the temp table.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: mark bundle as not supporting multiuse
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.