Javascript automatic function execution - Stack Overflow

If i'm writing event in this way, action executes when it should:document.getElementById('myE

If i'm writing event in this way, action executes when it should:

 document.getElementById('myElem').onmousedown = (e) => {
     console.log('fired!')
    }

But if i'm writing same stuff in other way, action executes when page is loaded, once:

let HandleEvent = (event) => {
 console.log('fired!')
}
document.getElementById('myElem').onmousedown = HandleEvent(event)

UPD: Ofcourse it is just example, HandleEvent function will have much more plex logic.

My questions is:

  1. Why?
  2. How to make it work properly?

If i'm writing event in this way, action executes when it should:

 document.getElementById('myElem').onmousedown = (e) => {
     console.log('fired!')
    }

But if i'm writing same stuff in other way, action executes when page is loaded, once:

let HandleEvent = (event) => {
 console.log('fired!')
}
document.getElementById('myElem').onmousedown = HandleEvent(event)

UPD: Ofcourse it is just example, HandleEvent function will have much more plex logic.

My questions is:

  1. Why?
  2. How to make it work properly?
Share Improve this question edited Jul 7, 2016 at 11:22 Src asked Jul 7, 2016 at 11:10 SrcSrc 5,5426 gold badges32 silver badges61 bronze badges 1
  • 5 onmousedown expects a function reference, but what you're passing is the returned value of the function (which is undefined) because you're invoking it. Try document.getElementById('myElem').onmousedown = HandleEvent instead. – haim770 Commented Jul 7, 2016 at 11:12
Add a ment  | 

6 Answers 6

Reset to default 6
document.getElementById('myElem').onmousedown = HandleEvent;

Would do what you want; you should not execute the handler when assigning it. You should just assign the function reference.

Then you can also assign it as the onload handler, or even call it yourself (if you are not relying on the event object)

document.getElementById('myElem').onmousedown = HandleEvent;

To pass additional arguments you could use bind:

document.getElementById('myElem').onmousedown = HandleEvent.bind(null, argument1, argument2);

...or you could use a factory function:

document.getElementById('myElem').onmousedown = createHandler();

function createHandler() {
  var a = calculateA();
  var b = calculateA();
  return function handleEvent() {
     //use a and/or b
  };
}

There are other ways too. Precise code will depend on your use case.

You don't need the event.

let HandleEvent = (event) => 'fired!';
document.getElementById('myElem').onmousedown = HandleEvent

Remove the event

let HandleEvent = (event) => {
 console.log('fired!')
}
document.getElementById('myElem').onmousedown = HandleEvent

If you want to pass an attribute

let HandleEvent = (event) => {
 console.log('fired!')
}
document.getElementById('myElem').onmousedown = HandleEvent.bind(attribute)

What you did was assigning the result of HandleEvent() to the event, instead you should assign the function to it.

Correct statement:

document.getElementById('myElem').onmousedown = HandleEvent;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745644024a4637863.html

相关推荐

  • Javascript automatic function execution - Stack Overflow

    If i'm writing event in this way, action executes when it should:document.getElementById('myE

    22天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信