简体   繁体   English

Dartpad html - 如何在重新运行时删除事件监听器?

[英]Dartpad html - how to remove event listeners on re-run?

In Dartpad with html ( https://dartpad.dev/html ), I am experimenting with the following code snippet:在带有 html ( https://dartpad.dev/html ) 的 Dartpad 中,我正在尝试以下代码片段:

import 'dart:html';

void main() {
  final header = querySelector('#header');
  header?.text = "Hello, World!";
  
  var streamobject = document.onSelectionChange.listen(handleSelectionChange);
  
  // streamobject.cancel() when re-run
  
}

void handleSelectionChange(Event e) {
  // do sth with range
}

However, when I run this code snippet again, the event listener from the previous run is not detached.但是,当我再次运行此代码片段时,之前运行的事件侦听器并未分离。 Unfortunately, it seems that it is not possible to access the Run button in Dartpad and do something like this:不幸的是,似乎无法访问 Dartpad 中的“运行”按钮并执行以下操作:

ButtonElement btn = querySelector('#run-button');
btn.onClick.listen((event) => streamobject.cancel());

Is there a way to delete all event listeners from document upon re-execution in dartpad?有没有办法在 dartpad 中重新执行时从文档中删除所有事件侦听器?

A page refresh cleaning cache does the job ( Cmd + Shift + R on Mac, or Ctrl + F5 on Windows).页面刷新清理缓存可以完成这项工作(在 Mac 上为Cmd + Shift + R ,在 Windows 上为Ctrl + F5 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM