M
M
MeinJun2020-05-02 16:57:52
JavaScript
MeinJun, 2020-05-02 16:57:52

How to implement date subtraction function with mouseover/out in js?

How to implement a function in js that records dates on mouseover and mouseout on any link in html, and subtracts the first one from the second one (In fact, find out the time the mouse spent over the link)?

I found something similar, on learn js, but I just can’t convert it to my task:

function mouselog(event) {
  let d = new Date();
  text.value += `${d.getHours()}:${d.getMinutes()}:${d.getSeconds()} | ${event.type} [target: ${event.target.id}]\n`.replace(/(:|^)(\d\D)/, '$10$2');
  text.scrollTop = text.scrollHeight;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2020-05-02
@MeinJun

let link = document.querySelector('.link');
link.addEventListener('mouseover', () => {
    let start = new Date();

    link.addEventListener('mouseout', () => {
        let end = new Date();
        console.log(end - start);
    });
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question