Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6009165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:55:54+00:00 2026-05-23T01:55:54+00:00

Writing a code that does the following: Adds a mouse listener to a web-page

  • 0

Writing a code that does the following:

  1. Adds a mouse listener to a web-page on load
  2. Checks what element the mouse is positioned over
  3. If the mouse is positioned over an anchor tag then execute a certain functionality if it stays there for a certain period of time before leaving. For now am just arbitrarily using 10000 ms

Below is a sample code that I’m using as a testbed

<html>
<head></head>
<title>A test page</title>
<body>

    <script type='text/javascript'>
    document.addEventListener("mouseover", checkElement, false);

    function checkElement(ev){
         var elm = ev.target || ev.srcElement;
            if(elm.tagName === 'A'){
            var focusTime = new Date().getTime();
            elm.addEventListener("mouseout", function() {tellTime(focusTime)}, false);
         }
    }

    function tellTime(focusTime){
        var blurTime = new Date().getTime();
        if ((blurTime - focusTime) > 10000) {
            alert ('You spent a long time there');
        }
    }
    </script>

    <a href="www.google.co.in">This is a hyperlink</a>
    <p> This is just some bloody text </p>
</body>

</html>

When testing what I have found is that when the alert is fired it gets fired multiple times; twice, thrice or even more. I wanted to know why this could be happening and what I can do to avoid firing the alert multiple times for the same element.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:55:55+00:00Added an answer on May 23, 2026 at 1:55 am

    Repeated event bindings… Try the following code out.

    <html>
    <head></head>
    <title>A test page</title>
    <script type='text/javascript'>
        document.addEventListener("mouseover", checkElement, false);
    
        function checkElement(ev){
            var elm = ev.target || ev.srcElement;
            if(elm.tagName === 'A'){
                var focusTime = new Date().getTime();
                elm.addEventListener("mouseout", tellTime, false);
            }
            function tellTime(){
                elm.removeEventListener("mouseout", tellTime, false);
                var blurTime = new Date().getTime();
                if ((blurTime - focusTime) > 2000) {
                    alert ('You spent a long time there');
                }
            }
    }
    
    
    </script>
    <body>
        <a href="www.google.co.in">This is a hyperlink</a>
        <p> This is just some bloody text </p>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing code that checks for the permission to write to and delete
I am in the following situation: I am writing code for a kernel that
I'm writing code that looks similar to this: public IEnumerable<T> Unfold<T>(this T seed) {
I find myself writing code that looks like this a lot: set<int> affected_items; while
In writing the code that throws the exception I asked about here , I
I'm writing C# code that uses the windows IP Helper API. One of the
I'm writing some code that id like to be able to work with any
I'm writing some code that handles logging xml data and I would like to
I'm writing some code that uses dynamic shared libraries as plugins. My command line
On Fedora Core 7, I'm writing some code that relies on ARG_MAX . However,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.