以编程方式设置 OnDetach 事件处理程序
Set OnDetach event handler programmatically
我正在构建的应用程序使用外部库 EasyTimer.js。它对计时器很有用。我使用 HTML 小部件来显示自计时器启动以来已经过去了多少时间,因此它看起来像这样:
它真的很好用!我遇到的唯一问题是当页面分离时我需要停止计时器;那是因为记录将被更改,因此计时器需要重新启动。不这样做,会导致与计时器冲突。
在 HTML 小部件 onAttach 事件中,我有这个:
//start running timer in the HTML widget
var runningTimer = new Timer();
runningTimer.start({precision: 'seconds', startValues: {seconds: startSeconds}});
runningTimer.addEventListener('secondsUpdated', function (e) {
$('#timerDiv').html(timerRun.getTimeValues().toString());
});
//attach event listener to page onDetach event to stop timer
widget.root.getElement().addEventListener("unload", function(){
runningTimer.stop();
console.log("Timer stopped");
)};
代码的顶部有效,底部没有任何作用。我已经检查了 widget logic documentation,但没有关于这是否可能的提示。
我觉得这不可能,我开始抓狂了;尽管如此,我想在这里咨询专家,以防万一有解决方案,但我还没有找到。
对于您对此事的宝贵时间和意见,在此先感谢!
到目前为止,无法以编程方式设置 onDetach 事件处理程序;不过,我通过在页面上实现自定义 属性 为您找到了一个可能的解决方案。在本示例中,我使用了 Dynamic 类型的 属性 并将其命名为 'Timer'。我没有为此实现 Jquery 库,而是使用了 HTML 小部件选项。这是 html 小部件 onAttach 事件代码:
//start running timer in the HTML widget
widget.root.properties.Timer = new Timer();
var runningTimer = widget.root.properties.Timer;
runningTimer.start({precision: 'seconds', startValues: {seconds: 0}});
runningTimer.addEventListener('secondsUpdated', function (e) {
widget.html = runningTimer.getTimeValues().toString();
});
页面onDetach事件代码如下:
widget.root.properties.Timer.stop();
希望这对您有用或让您朝着正确的方向前进。
我正在构建的应用程序使用外部库 EasyTimer.js。它对计时器很有用。我使用 HTML 小部件来显示自计时器启动以来已经过去了多少时间,因此它看起来像这样:
它真的很好用!我遇到的唯一问题是当页面分离时我需要停止计时器;那是因为记录将被更改,因此计时器需要重新启动。不这样做,会导致与计时器冲突。
在 HTML 小部件 onAttach 事件中,我有这个:
//start running timer in the HTML widget
var runningTimer = new Timer();
runningTimer.start({precision: 'seconds', startValues: {seconds: startSeconds}});
runningTimer.addEventListener('secondsUpdated', function (e) {
$('#timerDiv').html(timerRun.getTimeValues().toString());
});
//attach event listener to page onDetach event to stop timer
widget.root.getElement().addEventListener("unload", function(){
runningTimer.stop();
console.log("Timer stopped");
)};
代码的顶部有效,底部没有任何作用。我已经检查了 widget logic documentation,但没有关于这是否可能的提示。
我觉得这不可能,我开始抓狂了;尽管如此,我想在这里咨询专家,以防万一有解决方案,但我还没有找到。
对于您对此事的宝贵时间和意见,在此先感谢!
到目前为止,无法以编程方式设置 onDetach 事件处理程序;不过,我通过在页面上实现自定义 属性 为您找到了一个可能的解决方案。在本示例中,我使用了 Dynamic 类型的 属性 并将其命名为 'Timer'。我没有为此实现 Jquery 库,而是使用了 HTML 小部件选项。这是 html 小部件 onAttach 事件代码:
//start running timer in the HTML widget
widget.root.properties.Timer = new Timer();
var runningTimer = widget.root.properties.Timer;
runningTimer.start({precision: 'seconds', startValues: {seconds: 0}});
runningTimer.addEventListener('secondsUpdated', function (e) {
widget.html = runningTimer.getTimeValues().toString();
});
页面onDetach事件代码如下:
widget.root.properties.Timer.stop();
希望这对您有用或让您朝着正确的方向前进。