How to make a function run while the button is HOLDED? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to make a function run while the button is HOLDED?

I want to run a function when the users hold their finger on the button...

19th Oct 2018, 8:16 AM
Jingga Sona
Jingga Sona - avatar
5 Answers
+ 6
/* You need to use ontouchstart and ontouchend events (or, for desktop, onmousedown and onmouseup). Do it like this: */ var start; var end; var elapsed; button.ontouchstart = function() { start = (new Date).getTime(); // Get the current time, in milliseconds }; button.ontouchend = function() { end = (new Date).getTime(); // Same here elapsed = (end - start) / 1000; // Calculate the number of seconds console.log(elapsed + " seconds"); };
19th Oct 2018, 8:25 AM
Rowsej
Rowsej - avatar
+ 3
Jingga Sona no worries again! I just thought of this now. And, you can also attach the function to any element (img, div, etc.). P.S. Is it your 3:30 now?
19th Oct 2018, 8:29 AM
Rowsej
Rowsej - avatar
+ 1
Rowsej Thank You!!!๐Ÿ‘๐Ÿ™๐Ÿ˜Š You are my best supporters๐Ÿ˜Š
19th Oct 2018, 8:27 AM
Jingga Sona
Jingga Sona - avatar
+ 1
yes... this is 3:35pm here Rowsej
19th Oct 2018, 8:32 AM
Jingga Sona
Jingga Sona - avatar
0
just google it...
19th Oct 2018, 9:50 AM
ajay raja
ajay raja - avatar