Tuesday, 15 July 2014

javascript - Execute a function differently depending on when it was first run -



javascript - Execute a function differently depending on when it was first run -

i want piece of code execute if run within 6 seconds period after first time run.

i thinking doing so:

var withinsix = false; function ondeletekeyup(){ withinsix = true; if(withinsix){ //interaction code here settimeout(function(){ withinsix = false; }, 6000); } });

is there improve way this?

alternatively, without using timers, track when first called:

var called = -1; function ondeletekeyup(){ // track current time on first call. (in ms) if (called === -1){ called = (new date()).gettime(); } // compare current time time of first call. (6s = 6000ms) if ((new date()).gettime() - called < 6000){ // within 6 seconds... } else { // after 6 seconds... } }

javascript

No comments:

Post a Comment