mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-10-10 07:23:23 +08:00
添加setTimeout和clearTimeout支持
This commit is contained in:
@@ -42,7 +42,6 @@ const quickcommand = {
|
||||
|
||||
// setTimout 不能在 vm2 中使用,同时在 electron 中有 bug
|
||||
sleep: function (ms) {
|
||||
var start = new Date().getTime();
|
||||
try {
|
||||
// node 16.13.1
|
||||
child_process.execSync(getSleepCodeByShell(ms), {
|
||||
@@ -50,23 +49,26 @@ const quickcommand = {
|
||||
windowsHide: true,
|
||||
});
|
||||
} catch (ex) {}
|
||||
var end = new Date().getTime();
|
||||
return end - start;
|
||||
return;
|
||||
},
|
||||
|
||||
// 重写 setTimeout
|
||||
setTimeout: function (callback, ms) {
|
||||
var start = new Date().getTime();
|
||||
child_process.exec(
|
||||
const child = child_process.exec(
|
||||
getSleepCodeByShell(ms),
|
||||
{
|
||||
timeout: ms,
|
||||
},
|
||||
() => {
|
||||
var end = new Date().getTime();
|
||||
callback(end - start);
|
||||
if (child.signalCode === "SIGKILL") return;
|
||||
callback();
|
||||
}
|
||||
);
|
||||
return child.pid;
|
||||
},
|
||||
|
||||
clearTimeout: function (pid) {
|
||||
kill(pid, "SIGKILL");
|
||||
},
|
||||
|
||||
asyncSleep: async function (ms) {
|
||||
|
@@ -137,6 +137,9 @@ let getSandboxFuns = () => {
|
||||
os,
|
||||
child_process,
|
||||
quickcomposer,
|
||||
// timeout
|
||||
setTimeout: quickcommand.setTimeout,
|
||||
clearTimeout: quickcommand.clearTimeout,
|
||||
};
|
||||
Object.keys(shortCodes).forEach((f) => {
|
||||
sandbox[f] = shortCodes[f];
|
||||
|
Reference in New Issue
Block a user