gpt4 book ai didi

javascript - 如何删除 casperjs 事件监听器

转载 作者:行者123 更新时间:2023-11-27 22:42:14 24 4
gpt4 key购买 nike

我有一个如下定义的监听器:

function loadFailed() {
casper.echo("load failed catched");
}

casper.on('load.failed', loadFailed);

... some code

casper.removeListener('load.failed', loadFailed);

//redefine loadFailed function
function loadFailed(e) {
flagLoadFailed = true;
}

b.casper.on('load.failed', loadFailed);

然后我让两个函数在//中工作,而不是用新的实例删除第一个实例。

是否有任何方法可以删除代码中某处的监听器(并可能恢复原始监听器)?

最佳答案

您是否尝试过类似的操作:-

casper.on('load.failed', loadFailed)
...
casper.removeListener('load.failed', loadFailed);

function loadFailed(e) {
...
}

对于测试脚本,您可以使用安装和拆卸,例如

casper.test.begin('Missing resources', 1, {

setUp: function () {
// Attach the resource listener
casper.on('resource.received', this.onResourceReceived);
},

tearDown: function () {
// Remove the resource listener
casper.removeListener('resource.received', this.onResourceReceived);
},

test: function (test) {
casper.start(...);

casper.run(function () {
test.done();
});
},

onResourceReceived: function (resource) {
if (resource.stage === 'end' && resource.status === 404) {
require('utils').dump(resource);
}
}
});

关于javascript - 如何删除 casperjs 事件监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38663528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com