If you reword your question to:
detect how long a variable has been set
then you can use new Date().getTime()
to get the number of ticks when the value changes and then the same when it changes back (other functions on Date() are available, eg get number of seconds or millseconds).
eg:
var t = null;
function start() { t = new Date().getTime(); }
function stop() { t = (new Date().getTime()) - t; }
start();
setTimeout(function() { stop(); }, 1250);
you set/reset/check t
when your condition changes.
You can combine the two start/stop above by checking for null
to see if it has started, eg:
var t = null;
function action() {
if (t == null)
{
t = new Date().getTime();
} else {
t = (new Date().getTime()) - t;
// do something
onValueChange(t);
}
}
action();
setTimeout(function() { action(); }, 1250);
As an example, in javascript/jquery, using an HTML button, you can record the mousedown
and compare when you mouseup
to see how long a button has been pressed, eg:
var t = null;
$("#btn")
.on("mousedown", function() {
t = new Date().getTime(); // current ticks
}).on("mouseup", function() {
t = new Date().getTime() - t; // delta ticks (could use seconds or milliseconds)
// Do something with t
$("#result").text("You pressed for " + t + " ticks.");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id='btn'>press me</button>
<div id='result'></div>
1
solved Check if condition is true for a longer time period – not only at a specific time