Why copy function is not working inside setTimeout?

浪子不回头ぞ 提交于 2019-12-05 04:53:17

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.

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

Inspired by the mention of with in this answer, I discovered that you can use it to make copy() available in setTimeout() and other callbacks, instead of having to create a global reference to it:

with ({ copy }) { setTimeout(() => copy("copied!"), 0) }

copied! will now be on your clipboard. Unfortunately, this trick doesn't seem to work in Firefox's console.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!