search

Home  >  Q&A  >  body text

How can I use javascript to record how long the user stays on the page?

I want to write a js that counts how long users stay on the page. There are currently two methods, but neither is ideal.

  1. Timer, use ajax to send data to the database at certain intervals. This method cannot guarantee accuracy and will affect efficiency

  2. Use the onunload event to send data when the user closes the window. However, a dialog box will pop up in chrome to prompt whether to leave the page, which is very unfriendly to users visiting the website

How to solve it

给我你的怀抱给我你的怀抱2795 days ago536

reply all(1)I'll reply

  • 滿天的星座

    滿天的星座2017-05-19 10:42:39

    If you want to be very accurate, you can consider using websocketto maintain a long connection with the backend?

    reply
    0
  • Cancelreply