SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2002
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How can I removeEventListener ?

    Now that I've used
    document.addEventListener("mousemove", function(event) {move (objectID)}, false);

    now I cannot removeEventListener . I've tried many things:

    ------------------------------------------------------------------------------------
    document.removeEventListener("mousemove", function(event) {move (objectID)}, false);

    document.removeEventListener("mousemove", move, false);

    ...
    ------------------------------------------------------------------------------------

    But none of them worked. How can I fix this?

    Here is the example written just for this question.

    Please help, I really need this.

  2. #2
    JavaScript Guru (Big Ego) Arielladog's Avatar
    Join Date
    Jul 1999
    Location
    SC, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Rok,

    Without testing any code, I can't swear that this is the problem, but I'll take a stab at it (since no one else on here seems to be). When you add an event listener, for the second argument, you use a function. Well, if you are removing that event listener, you need to be able to reference that same function (not just a copy of it, but the exact same function). So, when you try with:

    document.removeEventListener("mousemove", function(event) {move (objectID)}, false);

    You are referencing a function with the same code, but you are not referencing that same function. It's like how:

    function x(){
    alert("me");
    }

    is different from:

    function y(){
    alert("me");
    }

    document.removeEventListener("mousemove", move, false);

    For this one, from what I can tell you really didn't use the move function when you added the event listener. You created a function that uses the move function.

    At any rate, what I'd try first is to create a global function and use that as the reference when you add and remove the event listener. Note that the event object you pass should be passed automatically so all you'll probably need is:

    document.addEventListener("mousemove", moveIt, false);

    As long as moveIt has event as its first parameter. There's also another way to access the event argument (if perhaps this doens't work...I don't think you'll run into that problem). Anyway, post back if this works or you still need more help.

    aDog

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2002
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, thanks for the help, but I really don't understand this event thing and there are a few things that are blurry for me.

    But I found a workaround:
    I used a global variable. Instead of passing an argument to a function that passes that argument to another function , I used a global variable. In that variable was the info that I wanted to pass to the function using arguments. But instead I referenced that variable from the function.

    So thanks anyway!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •