JS Intl Object: A Complete Introduction and Its Applications in Chinese

JS Intl Object: A Complete Introduction and Its Applications in Chinese This article was published on Friday, September 13, 2019, at 14:25, categorized under JS API. It has been read 50,018 times, 14 times today, with 22 comments. by zhangxinxu from https://www.zhangxinxu.com/wo...

Code: Dispatching a CustomEvent

const event = new CustomEvent('show', {
  detail: { message: payload, sentAt: new Date().toLocaleTimeString() }
});

window.dispatchEvent(event);
Result
Waiting for an event...

Code: Listening for the Event Payload

window.addEventListener('show', (event) => {
  render(event.detail.message, event.detail.sentAt);
});
Event log
No events yet.