gpt4 book ai didi

javascript - 想要在加载函数之外使用 Clippy.JS 的代理对象

转载 作者:行者123 更新时间:2023-12-03 03:37:16 36 4
gpt4 key购买 nike

我正在使用Clippy.JS,这是一个有趣的小型Javascript库,可以复活微软的助手。

假设我想召唤巫师梅林:

clippy.load('Merlin', function(agent){
// do anything with the loaded agent
agent.show();
agent.moveTo(100,100);
agent.speak("Arthur, you are the chosen one to slay the dragon");
});

这很有效并且很容易实现。当我想要移动 Merlin 时,问题就出现了:

$( "#target" ).click(function() {
agent.moveTo(333,333);
});

代理对象未在此范围内初始化,我不知道加载后如何检索代理对象。

控制台出现此错误:

Uncaught ReferenceError: agent is not defined 

最佳答案

agent 不是全局变量,仅在代理回调函数中可用。

要解决这个问题,您需要在回调函数之外创建一个变量,然后使用它来全局操作代理。

以下内容应该有效。

//define the personal agent outside the callback function
let merlin;

clippy.load('Merlin', function(agent){
merlin = agent;
merlin.show();
merlin.moveTo(100,100);
merlin.speak("Arthur, you are the chosen one to slay the dragon");
});

$( "#target" ).click(function() {
merlin.moveTo(333,333);
});

关于javascript - 想要在加载函数之外使用 Clippy.JS 的代理对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45798419/

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