Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. /**
  2. * isActionTimeoutExpired check the time since the cache was set
  3. */
  4. function isActionTimeoutExpired(action) {
  5. var actions = easy.context.site.actions;
  6. var expired = true;
  7.  
  8. if (actions && actions.length) {
  9. easy.utils.each(actions, function (currAction) {
  10. if (currAction.name === action) {
  11. expired = isExpired(new Date(currAction.set).getTime() / 1000,
  12. currAction.expiresMin);
  13. }
  14. });
  15. }
  16.  
  17. return expired;
  18. }
  19.  
  20. /**
  21. * setActionsTimeout Set timeout for action to be expired
  22. * @param {number} min Time in minutes
  23. */
  24. function setActionTimeout(action, min) {
  25. if (!easy.context.site.actions) {
  26. easy.context.site.actions = [];
  27. }
  28.  
  29. function pushOrUpdate (data) {
  30. if (data.hasOwnProperty('name')) {
  31. var updated = false;
  32. easy.utils.each(easy.context.site.actions, function (obj) {
  33. if (obj.hasOwnProperty('name') && obj.name === data.name) {
  34. obj.set = new Date();
  35. obj.expiresMin = data.expiresMin;
  36. updated = true;
  37. }
  38. });
  39.  
  40. if (!updated) {
  41. easy.context.site.actions.push({
  42. name: action,
  43. set: new Date(),
  44. expiresMin: min
  45. });
  46. }
  47. }
  48. }
  49.  
  50. pushOrUpdate({
  51. name: action,
  52. set: new Date(),
  53. expiresMin: min
  54. });
  55.  
  56. easy.context.save();
  57. }
  58. /**
  59. * Check if the saved timestamp has expired
  60. * @param {Int} timestamp Unix timestamp
  61. * @param {Int} duration Minute as unit
  62. * @return {Boolean}
  63. */
  64. function isExpired(timestamp, duration) {
  65. var currentTimestamp = getTimestamp();
  66. var dur_inSecond = duration * 60;
  67. return (currentTimestamp - timestamp) >= dur_inSecond ? true : false;
  68. }
  69.  
  70. function getTimestamp () {
  71. return Math.round(new Date().getTime() / 1000);
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement