Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法访问javascript中回调外部的对象值_Javascript_Callback - Fatal编程技术网

无法访问javascript中回调外部的对象值

无法访问javascript中回调外部的对象值,javascript,callback,Javascript,Callback,我在弄清楚如何访问回调函数之外的值时遇到了问题。单击“播放”按钮时,回调将运行。将有其他函数使用回调函数中的值,因此我希望能够根据用户选择的其他按钮在回调函数之外访问这些值。它总是以未定义的形式返回,这是有意义的,因为它在用户按下“播放”之前触发。如何将其传递给可以在整个脚本中访问的变量?我尝试将整个回调函数赋给一个变量,但是回调函数本身却没有定义。我尝试过使用return,但也没用。我是javascript新手,这让我很困惑,所以我非常感谢您提供的任何帮助 谢谢, 桑迪 //jsonp的回调

我在弄清楚如何访问回调函数之外的值时遇到了问题。单击“播放”按钮时,回调将运行。将有其他函数使用回调函数中的值,因此我希望能够根据用户选择的其他按钮在回调函数之外访问这些值。它总是以未定义的形式返回,这是有意义的,因为它在用户按下“播放”之前触发。如何将其传递给可以在整个脚本中访问的变量?我尝试将整个回调函数赋给一个变量,但是回调函数本身却没有定义。我尝试过使用return,但也没用。我是javascript新手,这让我很困惑,所以我非常感谢您提供的任何帮助

谢谢, 桑迪


//jsonp的回调函数
功能手柄入口(入口){
控制台日志(条目);
console.log(entry.results[0]);
console.log(entry.results[0].headword);
document.getElementById(“blanks”).innerHTML=entry.results[0]。headword;
document.getElementById(“定义”).innerHTML=entry.results[0]。senses[0]。定义;
var myHeadword=entry.results[0]。headword;
var entryInfo={};
entryInfo.definition=entry.results[0]。感官[0]。定义
log(“这是entryInfo定义:”+entryInfo.definition)
entryInfo.part\u of\u speech=entry.results[0]。part\u of\u speech
log(“这是entryInfo词性:“+entryInfo.part\u of\u词性”)
}
console.log(entry.results[0].senses[0].definition);
函数loadWord(){
//获取字典索引范围内的随机数,并将其分配给变量randomOffset
var randomOffset=Math.floor(Math.random()*(21178));
console.log(“偏移量:+randomOffset”);
//使用API查询URL中的变量randomOffset获取单个随机条目
变量url=”https://api.pearson.com/v2/dictionaries/lasde/entries?offset=“+randomOffset+”&limit=1&jsonp=handleEntry”;
日志(headwordapi:+url);
//创建一个新的脚本元素
var script_element=document.createElement('script');
//将其源代码设置为JSONPAPI
script_element.src=url;
//将脚本元素粘贴到页面中
document.getElementsByTagName('head')[0].appendChild(script_元素);
}
//这是“放弃”按钮调用的函数,需要从
//回调,但一直返回未定义。
函数showTheInfo(entryInfo){
log(“这是定义:”+entryInfo.definition)
警报('这是您的第二个参数'+entryInfo.definition');
log(“这是词性:+entryInfo.part\u of theu speech”)
警报(“这是您的第三个参数”+entryInfo.part of theu speech);
document.getElementById(“definition”).innerHTML=entryInfo.part of theu speech;
}
再玩一次
帮助

可能是这样的

<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="utf-8">
<script>
 //callback function for jsonp
 function handleEntry (entry) {
console.log(entry);
console.log(entry.results[0]);
console.log(entry.results[0].headword);
document.getElementById("blanks").innerHTML = entry.results[0].headword;
document.getElementById("definition").innerHTML = entry.results[0].senses[0].definition;
var myHeadword = entry.results[0].headword;
var toBeUsedForCallBack = entry.results[0].senses[0].definition; 
ThisIsTheSecondCallBackFunction(toBeUsedForCallBack)
}

console.log(entry.results[0].senses[0].definition);

function loadWord() {
// Get a random number within the range of the dictionary index and assign it to the variable randomOffset
var randomOffset = Math.floor(Math.random() * (21178));
console.log("offset: " + randomOffset);
// Use the variable randomOffset in the API query URL to get a single random entry
var url = "https://api.pearson.com/v2/dictionaries/lasde/entries?offset=" + randomOffset + "&limit=1&jsonp=handleEntry";
console.log("headword API: " + url);


// Create a new script element
var script_element = document.createElement('script');
// Set its source to the JSONP API
script_element.src = url;
// Stick the script element in the page <head>
    document.getElementsByTagName('head')[0].appendChild(script_element);
ThisIsTheFirstCallBackFunction(url)
}

function ThisIsTheFirstCallBackFunction (yourFirstParameterHere){ alert('this is your 1st parameter ' + yourFirstParameterHere);}
function ThisIsTheSecondCallBackFunction (yourSecondParameterHere){ alert('this is your 2nd parameter ' + yourSecondParameterHere);}
    </script>
</head>
<body>
    <div class="row">
        <div class="col-md-6 col-sm-4">
            <div id="blanks" class="blanks align-me"></div>
        </div>
        <div class="col-md-6 col-sm-4">
            <div id="definition" class="definition align-me"></div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12 col-sm-12 align-me">
            <input onclick="loadWord()" id="loadWordButton" value="Play" type="button" class="btn btn-custom btn-md">
            <button type="button" class="btn btn-custom btn-md">Give Up</button>
            <button type="button" class="btn btn-custom btn-md">Play Again</button>
            <button type="button" class="btn btn-custom btn-md">Help</button>
        </div>
    </div>
</div>
</body>

//jsonp的回调函数
功能手柄入口(入口){
控制台日志(条目);
console.log(entry.results[0]);
console.log(entry.results[0].headword);
document.getElementById(“blanks”).innerHTML=entry.results[0]。headword;
document.getElementById(“定义”).innerHTML=entry.results[0]。senses[0]。定义;
var myHeadword=entry.results[0]。headword;
var toBeUsedForCallBack=entry.results[0]。senses[0]。定义;
这是SecondCallback函数(用于回调)
}
console.log(entry.results[0].senses[0].definition);
函数loadWord(){
//获取字典索引范围内的随机数,并将其分配给变量randomOffset
var randomOffset=Math.floor(Math.random()*(21178));
console.log(“偏移量:+randomOffset”);
//使用API查询URL中的变量randomOffset获取单个随机条目
变量url=”https://api.pearson.com/v2/dictionaries/lasde/entries?offset=“+randomOffset+”&limit=1&jsonp=handleEntry”;
日志(headwordapi:+url);
//创建一个新的脚本元素
var script_element=document.createElement('script');
//将其源代码设置为JSONPAPI
script_element.src=url;
//将脚本元素粘贴到页面中
document.getElementsByTagName('head')[0].appendChild(script_元素);
这是第一个调用函数(url)
}
函数ThisIsTheFirstCallBackFunction(yourFirstParameterHere){alert('这是您的第一个参数'+yourFirstParameterHere);}
函数ThisIsTheSecondCallBackFunction(您的第二个参数在这里){alert('这是您的第二个参数'+您的第二个参数在这里);}
放弃
再玩一次
帮助

谢谢。这很有帮助。它似乎将外部函数链接到回调(这在其他方面很有用)。我尝试在一个函数中使用这些值进行处理,该函数在不同时间按下另一个按钮(如放弃)时调用。我想我正在尝试将回调中的值转换成全局变量,我可以从任何函数调用它们。我想我可能解释得不太清楚,我编辑了op,添加了一个从不同按钮调用的函数示例。我希望这更清楚一点。对不起,我是javascript新手,我不太确定如何解释它。是的,将值放在全局变量中会起作用。其他函数也可以访问它。不过,我能从中得到一个全局变量吗?我尝试将函数赋给一个变量,结果函数未定义。我尝试返回一个变量,但它在回调函数之外仍然没有定义。我似乎无法以可用的形式获取值。在JS的顶部定义一个空变量,例如
var globalVariable=“”<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="utf-8">
<script>
 //callback function for jsonp
 function handleEntry (entry) {
console.log(entry);
console.log(entry.results[0]);
console.log(entry.results[0].headword);
document.getElementById("blanks").innerHTML = entry.results[0].headword;
document.getElementById("definition").innerHTML = entry.results[0].senses[0].definition;
var myHeadword = entry.results[0].headword;
var toBeUsedForCallBack = entry.results[0].senses[0].definition; 
ThisIsTheSecondCallBackFunction(toBeUsedForCallBack)
}

console.log(entry.results[0].senses[0].definition);

function loadWord() {
// Get a random number within the range of the dictionary index and assign it to the variable randomOffset
var randomOffset = Math.floor(Math.random() * (21178));
console.log("offset: " + randomOffset);
// Use the variable randomOffset in the API query URL to get a single random entry
var url = "https://api.pearson.com/v2/dictionaries/lasde/entries?offset=" + randomOffset + "&limit=1&jsonp=handleEntry";
console.log("headword API: " + url);


// Create a new script element
var script_element = document.createElement('script');
// Set its source to the JSONP API
script_element.src = url;
// Stick the script element in the page <head>
    document.getElementsByTagName('head')[0].appendChild(script_element);
ThisIsTheFirstCallBackFunction(url)
}

function ThisIsTheFirstCallBackFunction (yourFirstParameterHere){ alert('this is your 1st parameter ' + yourFirstParameterHere);}
function ThisIsTheSecondCallBackFunction (yourSecondParameterHere){ alert('this is your 2nd parameter ' + yourSecondParameterHere);}
    </script>
</head>
<body>
    <div class="row">
        <div class="col-md-6 col-sm-4">
            <div id="blanks" class="blanks align-me"></div>
        </div>
        <div class="col-md-6 col-sm-4">
            <div id="definition" class="definition align-me"></div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12 col-sm-12 align-me">
            <input onclick="loadWord()" id="loadWordButton" value="Play" type="button" class="btn btn-custom btn-md">
            <button type="button" class="btn btn-custom btn-md">Give Up</button>
            <button type="button" class="btn btn-custom btn-md">Play Again</button>
            <button type="button" class="btn btn-custom btn-md">Help</button>
        </div>
    </div>
</div>
</body>