1

I was building a slide-menu with javascript using classes and objects and a little bit of jquery as part of my efforts to learn javascript more deeply. Everything went right until I wanted to bind a mouseleave to the initiator of my menu. So here's my code block

 var el;
 function generate(obj){
  return function(){obj.slidein();}
  } 
 function slider(arg1,arg2){
 ...//Some junk
 el=this;
 for(i=0;i<this.nsubs;i++){ ...
 $("#"+this.id+i).bind('mouseleave',function(){setTimeout("generate(el)",500)});
  }
 ...
 }

Well, I get no error on firefox error console but somehow the slidein() function which I want to be attached to the mouseleave is not being called when mouse leaves the element in question.

Can Someone explain what I am doing wrong here?

2
  • el having what?? and also in for loop write ( for var i=0;... Commented May 20, 2011 at 10:34
  • @diEcho: I've got no idea on what you mean! Can you elaborate? Commented May 20, 2011 at 13:54

2 Answers 2

2

Try passing a function rather than a string to setTimeout:

$("#"+this.id+i).bind('mouseleave', function() {
   setTimeout(function() { 
      generate(el); 
   }, 500)
});
Sign up to request clarification or add additional context in comments.

1 Comment

(sorry for double comment I couldnt edit it within 5 mins, continuing: )executing:setTimeout(generate(el), 500); on firefox console, it works. BTW, I also found out that removing the " around the generate function in my above code block seems to work but only for the last element covered by the for loop!(but I want it to work for each element) Any help is still appreciated..
0

Alright, I sorted it out on my own, what I actually needed was a closure that returns a function with 'setTimeout(generate(ObjPassedToClosure))'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.