本文介绍了为什么复制功能在setTimeout中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 setTimeout 内尝试复制时,Chrome会抱怨。

Chrome complains when I try to copy inside setTimeout.

setTimeout(function () { copy('a') }, 0)

Uncaught ReferenceError: copy is not defined
    at <anonymous>:1:26

它不适用于窗口范围。

setTimeout(function () { window.copy('a') }, 0)

Uncaught TypeError: window.copy is not a function

有趣的是,如果我保留对复制的引用并重复使用它,它可以工作

Interestingly, if I keep the reference to copy and reuse it, it works

cc = copy;
setTimeout(function () { cc('a') }, 0);

在Firefox中,它不会抛出任何错误,但即使保存也不起作用参考。

In Firefox, it doesn't throw any error, but it doesn't work even with the saved reference.

为什么复制函数在 setTimeout ,这是一个错误?

Why copy function doesn't work inside setTimeout, is it a bug?

推荐答案

复制是其中一部分开发人员工具'命令行API 并且在浏览器控制台之外无法使用。例如,尝试在普通网页的JavaScript文件中执行命令时会出现同样的错误。

copy is part of the developer tools' Command Line API and is not available outside the browser console. For example, trying to execute the command in a JavaScript file that's part of a normal web page you'd get the same error.

当您调用 setTimeout 回调,执行上下文不再是控制台,因此 copy 不再存在。

When you invoke the command inside the setTimeout callback, the execution context is no longer the console so copy doesn't exist anymore.

这篇关于为什么复制功能在setTimeout中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 22:56
查看更多