0

How can I get the time between touchstart and touchend in javasscript.

    // define startTime variable
    var startTime = 0;

    function initi() {
        console.log('init');
        // Get a reference to our touch-sensitive element
        var touchzone = document.getElementById("myCanvas");
        // Add an event handler for the touchstart event
        touchzone.addEventListener("mousedown", startTouch, false);
        // You need mouseup to capture the stop event                          
        touchzone.addEventListener("mouseup", stopTouch, false);
    }

    function startTouch(event) {
        // You can access the event here too..
        // But for demo I will only get the current Time

        startTime = new Date().getTime();
    }

    function stopTouch(event) {
        // Get a reference to our coordinates div
        var can = document.getElementById("myCanvas");
        // Write the coordinates of the touch to the div
        if (event.pageX < x * 100 && event.pageY > y * 10) {
            // Calculate the duration, only if needed
            var duration = new Date().getTime() - startTime;

            bally -= 0.001 * duration; // use duration
        }

        // I hope bally if defined in the outer scope somewhere ;)                    
        console.log(event, x, bally);
        draw();
    }

The longer the time between touchstart and touchend the more the ball should move, but how can I get the time/length of the touchevent?

webdeb
  • 12,993
  • 5
  • 28
  • 44
Bodoppels
  • 406
  • 7
  • 24
  • it is unclear what was originally asked - it looks like the answer was integrated into the question. – allicarn Sep 09 '15 at 13:01

4 Answers4

2

var startTime = 0;
function start() {
   startTime = new Date().getTime();
}

function stop() {
  var duration = (new Date().getTime() - startTime);
  growBar(duration);
}

var clickEle = document.getElementsByTagName("button")[0];
var timerEle = document.getElementsByClassName("timer")[0];
var ms = document.getElementById('ms');
clickEle.onmousedown = start;
clickEle.onmouseup = stop;

function growBar(timerCount) {
    ms.innerHTML = timerCount;
    timerEle.setAttribute("style", "width:" + timerCount + "px;");
}
.bar {
  height: 10px;
  width: 100%;
  background: black;
}
.timer {
  width: 0;
  height: 100%;
  background: red;
}
<button id="button-id">Click This!</button>
<div><strong id="ms"></strong> ms</div>
<div class="bar">
  <div class="timer"></div>
</div>
webdeb
  • 12,993
  • 5
  • 28
  • 44
  • The reason I used an interval in my answer was this wording: "The longer the time between touchstart and touchend the more the ball should move" – allicarn Sep 08 '15 at 21:06
  • @allicarn setinterval is bad, here is why: http://stackoverflow.com/questions/5479762/will-setinterval-cause-browsers-to-hang?answertab=oldest#tab-top – webdeb Sep 08 '15 at 21:09
  • You can get rid of setInterval when you use requestAnimationFrame, because it will be called when the browser has time to do it.. – webdeb Sep 08 '15 at 21:14
  • Thank you for your helpful examples. I edit my question an tried to build your code into mine, but it doesnt work. It looks like you have experience in this area. What have I done false or what should I add? :) – Bodoppels Sep 09 '15 at 09:27
  • 1
    @Bodoppels, check the code again, hope you understand what you missed, (think about start -> end) – webdeb Sep 09 '15 at 11:07
  • @webdeb Many thanks!! Yeah I think I know the mistakes. :D – Bodoppels Sep 09 '15 at 11:13
1

You can start a Javascript Interval, of any number of milliseconds (depending on "smoothness" needed). See this below, mimicking what you can do, just using mousedown and mouseup events.

var clickEle = document.getElementsByTagName("button")[0];
var timerEle = document.getElementsByClassName("timer")[0];
var COUNTER_INCREMENT = 10; // In milliseconds
var timerCount = 0;
var timer = null;

clickEle.onmousedown = startTimer;
clickEle.onmouseup = stopTimer;

function startTimer() {
  timer = setInterval(incrementTimer, COUNTER_INCREMENT);
}

function incrementTimer() {
  timerCount += COUNTER_INCREMENT;

  // For Demo purposes only
  timerEle.innerHTML = timerCount;
}

function stopTimer() {
  clearInterval(timer);
  timerCount = 0;
}
<button>Click This!</button>
<p>
  <strong>How Long Was the Mouse Held Down for?</strong>
  <span class="timer">0</span> milliseconds
</p>

Or you can use requestAnimationFrame which is a little more optimal, but its availability depends on the browsers you're supporting. https://css-tricks.com/using-requestanimationframe/

var clickEle = document.getElementsByTagName("button")[0];
var timerEle = document.getElementsByClassName("timer")[0];
var COUNTER_INCREMENT = 10; // In milliseconds
var startTime = 0;
var timerCount = 0;
var timer = null;

clickEle.onmousedown = startTimer;
clickEle.onmouseup = stopTimer;

function startTimer() {
  timer = window.requestAnimationFrame(incrementTimer);
  startTime = new Date();
}

function incrementTimer() {
  var newTimerCount = new Date() - startTime;

  // Update the DOM sparingly
  if(newTimerCount - timerCount > COUNTER_INCREMENT) {
    // For Demo purposes only
    timerEle.innerHTML = timerCount = newTimerCount;
  }
  timer = window.requestAnimationFrame(incrementTimer)
}

function stopTimer() {
  cancelAnimationFrame(timer);
  timerCount = 0;
}
<button>Click This!</button>
<p>
  <strong>How Long Was the Mouse Held Down for?</strong>
  <span class="timer">0</span> milliseconds
</p>
allicarn
  • 2,859
  • 2
  • 28
  • 47
  • He needs only the time between start and end, the interval will try to call the function growBar each millisecond, it looks nice, but it's an overkill to solve the problem ;) – webdeb Sep 08 '15 at 20:43
  • :D but @allicarn, now you are calling incrementTimer each millisecond..? – webdeb Sep 08 '15 at 21:00
  • The OP did not specify how often he needed to query how long it has been. The increment can be adjusted obviously, and the count adjusted accordingly. Added a variable to demonstrate where it should be changed if needed. – allicarn Sep 08 '15 at 21:03
  • I copied the fancy stuff, to show how you can use only two function calls, ok you will not get the animation between, but it should be enough – webdeb Sep 08 '15 at 21:03
1

var clickEle = document.getElementsByTagName("button")[0];
var timerEle = document.getElementsByClassName("timer")[0];
var msEle = document.getElementById('ms');
var COUNTER_INCREMENT = 1; // In milliseconds
var timerCount = 0;
var timer = null;

clickEle.onmousedown = startTimer;
clickEle.onmouseup = stopTimer;

var ms = 0;
function startTimer() {
  ms = new Date().getTime();
  timer = setInterval(incrementTimer, COUNTER_INCREMENT);
}

function incrementTimer() {
  timerCount += COUNTER_INCREMENT;

  // For Demo purposes only
  timerEle.innerHTML = timerCount;
}

function stopTimer() {
  clearInterval(timer);
  timerCount = 0;
  msEle.innerHTML = new Date().getTime() - ms;
}
<button>Click This!</button>
<p>
  <strong>How Long Was the Mouse Held Down for?</strong>
  <span class="timer">0</span> milliseconds
</p>
<p><span id="ms"></span> real milliseconds</p>
webdeb
  • 12,993
  • 5
  • 28
  • 44
0

You have to define the startTime variable somewhere, in the "outer scope", where both functions have access to. the when you call the first function it will just change its value, the second function can access the variable too ;)

var startTime = 0;
function one() {
   startTime = new Date().getTime();
}

function two() {
  var duration = new Date().getTime() - startime;
  console.log(duration + 'ms between one() and two()');
}

var btn = document.getElementById('some-btn-id');
btn.onmousedown = one;
btn.onmouseup = two;
webdeb
  • 12,993
  • 5
  • 28
  • 44