javascript - SetimeOut 间隔因“无法将未定义或空值转换为对象”而失败
问题描述
我正在使用 Tampermonkey 编写用户脚本,但无法解决此错误,我们将不胜感激。
我很好地检测到键,空格键触发此功能,只要键保持在向下位置,该功能就会重复。控制台通常将输出写入或多或少 30 秒,然后出现 TypeError。
根据声誉限制,这是一个屏幕截图:
用户脚本:
// ==UserScript==
// @name TEST STUFF--------------------
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @run-at document-start
// @include http://*
// @include https://*
// @grant none
// ==/UserScript==
( function()
{
'use strict';
window.addEventListener ( "keydown", CaptureKeyPress );
window.addEventListener ( "keyup", CaptureKeyPress );
var Hotkeys =
{
perform: 32
};
var HotkeyToggle = false;
function CaptureKeyPress ( a )
{
if ( a.keyCode == Hotkeys.perform )
{
a.preventDefault();
a.stopPropagation();
a.cancelBubble = true;
a.stopImmediatePropagation();
if ( a.type == "keydown" && !HotkeyToggle )
{
console.clear();
HotkeyToggle = true;
perform();
}
if ( a.type == "keyup" && HotkeyToggle )
{
HotkeyToggle = false;
}
}
}
function perform()
{
if(HotkeyToggle == false) // exit
{
return 0
}
//do stuff...
console.info("working...");
if(HotkeyToggle == true) // continue after everything completes
{
setTimeout(() => {
perform()
}, 280);
return 0
}
return 1
}
} ) ();
解决方案
推荐阅读
- python-3.x - 我想在 python 中计算成绩,但如果任何个别科目的分数低于 35,我不想显示成绩
- javascript - Brave 浏览器无法与 Puppeteer 一起使用。Brave 无法读取和写入其 Data 目录
- css - grid-row: 1 / -1 实际上是如何工作的?(需要参考)
- mysql - 使用 nodejs 到 MariaDB 的 Angular POST
- ruby-on-rails - 用于 nil:NilClass 的 Rails 控制器 RSpec 未定义方法“编码”
- node.js - MongoDB 合并来自不同集合的两个聚合
- python - Python:如何用元组中的值替换多次出现的字符串
- css - 如何使用 SPFX 将 CSS 或 JavaScript 注入 SharePoint 现代页面
- parse-platform - 解析服务器令牌验证
- python - 如何将我的 csv 文件从列值转换为具有其值的新列..?