Javascript不等待返回值
Javascript not waiting for return value
一直认为javascript不是异步的,除非使用Ajax或其他东西。我没有使用 Ajax 调用或任何东西,但我的 JS 函数似乎没有等待返回值:
我有三行代码如下:
var areas = prepareComparer();
console.log('Areas');
console.log(areas);
我需要创建一个覆盖div 来起诉以下函数:
function prepareComparer() {
var comparorSelection = d3.select('body').append('div').attr('id', 'comparor').attr('style', 'line-height: 100px;position: fixed;top: 5%; left:5%;background-color: white;border-radius: 5px;text-align: center;z-index: 2;border-style:solid;border-width:1px;box-shadow: 10px 10px 5px #888888')
.transition().duration(500).style('height', '800px').style('width', $(window).width() * .9 + 'px').style('opacity', '1').each('end', function () {
//Waiting for end of transition to append the comparees: Otherwise, causes size adjustment problems since comparee sizes depend on the FULL height.
d3.select('#comparor').append('div').attr('id', 'comparee1').attr('style', 'position:absolute;top:0px;left:0px;width:' + $('#comparor').width() * .45 + 'px;height:' + $('#comparor').height() + 'px')
.append('svg');
d3.select('#comparor').append('div').attr('id', 'comparee2').attr('style', 'position:absolute;top:0px;left:' + $('#comparor').width() * .5 + 'px;width:' + $('#comparor').width() * .45 + 'px;height:' + $('#comparor').height() + 'px')
.append('svg');
d3.select('#comparor').append('div').attr('id', 'destroyComparor').text('Click Here To Go Back').on('click', function () {
d3.select('#comparor').transition().duration(500).style('width', '0px').style('height', '0px').style('opacity', '0').remove();
});
});
setTimeout(function () { console.log("Hello"); return ['#comparor #comparee1', '#comparor #comparee2'] }, 5000);
}
现在,为此,我的日志显示为:
Areas
undefined
Hello
忽略 d3 代码,因为注释或取消注释它似乎没有太大区别。可以肯定的是,Ive 引入了 5 秒的超时。因此,5 秒后我收到消息 Hello,这意味着在此之后也可能发生返回。我几乎立即收到未定义的消息。
我不太确定为什么会发生这种情况,并且多年来一直在尝试解码它。谁能帮忙?
编辑
想要在代码中完成新的div 创建(#comparee1 和 #comparee2)时返回数据。但是,由于异步的 d3 转换,引入了延迟。而且由于新的div 是在过渡完成后才创建的(有大小依赖关系),因此我的 main 函数无法立即选择新创建的div。
解决了使用 JavaScript Promise 的此问题:
function prepareComparer() {
promise = new Promise(function (resolve, reject) {
var comparorSelection = d3.select('body').append('div').attr('id', 'comparor').attr('style', 'line-height: 100px;position: fixed;top: 5%; left:5%;background-color: white;border-radius: 5px;text-align: center;z-index: 2;border-style:solid;border-width:1px;box-shadow: 10px 10px 5px #888888')
.transition().duration(500).style('height', '800px').style('width', $(window).width() * .9 + 'px').style('opacity', '1').each('end', function () {
//Waiting for end of transition to append the comparees: Otherwise, causes size adjustment problems since comparee sizes depend on the FULL height.
d3.select('#comparor').append('div').attr('id', 'comparee1').attr('style', 'position:absolute;top:0px;left:0px;width:' + $('#comparor').width() * .45 + 'px;height:' + $('#comparor').height() + 'px')
.append('svg');
d3.select('#comparor').append('div').attr('id', 'comparee2').attr('style', 'position:absolute;top:0px;left:' + $('#comparor').width() * .5 + 'px;width:' + $('#comparor').width() * .45 + 'px;height:' + $('#comparor').height() + 'px')
.append('svg');
d3.select('#comparor').append('div').attr('id', 'destroyComparor').text('Click Here To Go Back').on('click', function () {
d3.select('#comparor').transition().duration(500).style('width', '0px').style('height', '0px').style('opacity', '0').remove();
});
resolve(['#comparor #comparee1', '#comparor #comparee1']);
});
});
}
使用数据的函数将调用为:
promise.then(function (response) {
window[func](response[0], res, 'label', 'value');
});
只是想分享解决方案。
你的函数prepareComparer()
没有回报。在没有返回返回的 JavaScript 函数中,未定义返回。
setTimeout()
内的回报与prepareComparer()
无关。
在这里,除了 Ajax 之外,您还可以找到 JavaScript 中异步编程的另一个方面。
在 setTimeout 之前放一个返回值,因为函数将等待设置超时完成
看起来像这样
return setTimeout(function () { console.log("Hello"); return ['#comparor #comparee1', '#comparor #comparee2'] }, 5000);
抱歉不起作用,但由于您没有返回区域将始终无法确定。
对于异步问题跟踪器.js引入可能对您有用的 await 关键字
相关文章:
- Javascript返回值只在循环中返回一次
- 等待函数完成,然后将返回值分配给变量JS
- 如何使for循环等待回调返回值
- 等待来自Javascript模态的“返回值”
- JS Geolocation等待成功后返回值
- Javascript没有't等待存储返回值
- 如何等待数据库事务完成它的操作并在phonegap中返回值
- Ajax在返回值之前等待响应
- simplemodal确认示例等待返回值
- 函数中的 js 函数并等待返回值
- 等待CasperJS模块完成执行后再返回值
- 如何使angular在继续之前等待函数返回值
- 如何在返回值之前等待typescript / javascript承诺
- 返回一个等待的值返回一个承诺?(es7 async /等待)
- 如何使函数在返回true或false之前等待ajax调用返回值?
- Angular:在我在控制器中设置返回值之前,我怎么能等待服务中的承诺被解析呢?
- 如何等待异步方法的回调返回值
- 如何正确等待同步循环中触发的所有异步函数返回值 - JS
- 角度.js - 在控制器中等待服务从服务器返回值
- Javascript不等待返回值