我正在尝试使用http://abeautifulsite.net/notebook/87中的jQuery警报对话库而不是默认警报(在我看来这看起来非常糟糕).这似乎是一个很棒的库,但是没有一个如何使用jConfirm库的例子.
我需要做这样的事情:
function confirm() {
var result = false;
var response = false;
jConfirm('are you sure?', 'Confirmation Dialog',
function(r) {
result = r;
response = true;
return r;
});
if (response == true) {
alert(result);
return result;
}
else {
//wait for response
alert('hi');
}
}
Run Code Online (Sandbox Code Playgroud)
和我的.net按钮的来电:
我在插件的网站上发布了一条评论(就在今天早上),谷歌搜索了javascript并等待回调完成但没有结果.
在其余的javascript执行之前,有关如何正确使用回调来获取结果的任何想法?
谢谢.
我知道等待异步方法是愚蠢的,应该使用回调.但是如果第三方API强迫你同步呢?
我正在开发Chrome扩展程序,以防止用户访问已在另一个标签页中打开的网站.我基本上需要根据打开标签中的网址取消请求.我想这样使用chrome.webRequest.onBeforeRequest
:
function onBeforeRequest(details) {
var websiteAlreadyOpenInOtherTab;
// Here i want to set `websiteAlreadyOpenInOtherTab` by using the `chrome.tabs`
// API. It's asynchronous though and that's my problem. I cant return a value
// from an asynchronous method call.
if (websiteAlreadyOpenInOtherTab) {
return { cancel: true };
}
}
chrome.webRequest.onBeforeRequest.addListener(
onBeforeRequest,
{ urls: ['<all_urls>'], types: ['main_frame'] },
['blocking']);
Run Code Online (Sandbox Code Playgroud)
希望你在上面的代码中看到我的困境.我需要根据异步方法调用的结果返回一个对象.是否有可能实现这一目标?
我看过很多关于它的帖子,但我仍然不明白回调。我得到了这样的函数:
function getData (url) {
request(url, function (error, response, data) {
//...
return data;
}
});
}
var data = getData(url);
// I got undefined because it's not finished
if (data)
...
Run Code Online (Sandbox Code Playgroud)
我如何等待我的函数返回继续。我已经看到很多关于回调和承诺的事情,但在这种情况下如何使用它?例如我看过这篇文章,但我不明白答案。
我正在使用节点js