Firing and capturing custom events

前端 未结 4 581
青春惊慌失措
青春惊慌失措 2020-12-29 10:15

Imagine this scenario (it\'s really just a scenario):

  • I have a global counter that gets incremented on every mouse click.
  • when I reach 50 clicks, I
4条回答
  •  再見小時候
    2020-12-29 10:22

    The answers above seem to be deprecated.

    Create a new CustomEvent.

    var data = { x:20, y:30, rotationY: 60 },
        end = new CustomEvent('MOTION_FINISHED', { detail: data });
        this.dispatchEvent(end);
    

    It seems to only allow the property 'detail' to pass parameters though. I've also read 'bubbles' and 'cancelable' properties are available, but you didn't mention those in your question.

    To retrieve your parameters:

    function _handler(e){
        var x = (e.detail && e.detail.x) || 0;
    }
    

提交回复
热议问题