JavaScript Click Timer
Using a click timer in JavaScript to cancel or enable navigation within our Content Management System
Integral to our CMS
A major part of the Content Management System user interface has been adapted for how to handle clicks on links, as such we needed something flexible to work with.
In our system, if you click a link for under one second then it will allow you edit the content around it, however press and hold for a second or more and the link will be followed.
It uses a timer and timer interval to calculate the length of time that the item was clicked for.
JavaScript
var clickTimer = 0, clickTimerInterval;document.addEventListener("mousedown", function (event) { clickTimer = 0; clickTimerInterval = setInterval(function () { clickTimer += 1; }, 1000);}, false);document.addEventListener("click", function (event) { event.preventDefault(); if (event.target.tagName.toLowerCase = 'a') { if (clickTimer >= 1) { alert('You held for more than 1 second'); setWindowLocation(event.target); } else { alert('You held for less than 1 second'); } } else { }; clearInterval(parent.clickTimerInterval);}, false);function setWindowLocation(element) { var newUrl = element.toString(); window.location.replace(newUrl);}