I have a site that has add to cart functionality. I need to know how to check idle time of user so that i may check that if user add some products into cart and then don't buy and go for 20 minutes in idle then i need to clear his cart.
I am using sails app and I think must be any way to check cookie or something else that trigger after 20 minute if user is idle mood.
I try by setting cookie and save time for differentiate current time and next 20 minutes but unsuccessful for finding ideal time function that trigger and let us know for cookie expiration.
Please help if anyone know about this.
You can create a global policy that stores a datetime into a user's session and check that datetime with the current datetime, take the difference, and use that to call some service that clears the cart.
config/policies:
'*': 'checkCookieClearCart'
api/policies/checkCookieClearCart.js:
var _MS_PER_MIN = 60000; // in milliseconds
var _CART_TIMEOUT = 20; // in minutes
module.exports = function(req, res, next) {
if (!req.session.lastActivity) {
req.session.lastActivity = new Date();
} else {
var currDate = new Date(),
timeIdle = (req.session.lastActivity - currDate)/_MS_PER_MIN;
if (timeIdle > _CART_TIMEOUT) {
// do what you do to clear the user's cart (i.e. through controller action or service)
}
req.session.lastActivity = new Date();
}
return next();
}