如何进行简单的非块Javascript函数调用?例如:
//begin the program
console.log('begin');
nonBlockingIncrement(10000000);
console.log('do more stuff');
//define the slow function; this would normally be a server call
function nonBlockingIncrement(n){
var i=0;
while(i<n){
i++;
}
console.log('0 incremented to '+i);
}
Run Code Online (Sandbox Code Playgroud)
输出
"beginPage"
"0 incremented to 10000000"
"do more stuff"
Run Code Online (Sandbox Code Playgroud)
如何形成这个简单的循环以异步执行并通过回调函数输出结果?这个想法是不阻止"做更多的东西":
"beginPage"
"do more stuff"
"0 incremented to 10000000"
Run Code Online (Sandbox Code Playgroud)
我已经尝试过关于回调和延续的教程,但它们似乎都依赖于外部库或函数.他们都没有在真空中回答这个问题:如何编写Javascript代码是非阻塞的??
在询问之前,我非常努力地寻找这个答案; 请不要以为我没看.我发现的一切都是Node.js特定的([1],[2],[3],[4],[5])或其他特定于其他函数或库([6],[7],[8],[9],[10],[11]),特别是JQuery和setTimeout().请帮我用Javascript编写非阻塞代码,而不是Javascript和Node等Javascript编写的工具. 请在将其标记为重复之前重新阅读该问题.
如何在node.js中设置默认参数?
例如,假设我有一个通常看起来像这样的函数:
function(anInt, aString, cb, aBool=true){
if(bool){...;}else{...;}
cb();
}
Run Code Online (Sandbox Code Playgroud)
要调用它看起来像这样:
function(1, 'no', function(){
...
}, false);
Run Code Online (Sandbox Code Playgroud)
要么:
function(2, 'yes', function(){
...
});
Run Code Online (Sandbox Code Playgroud)
但是,似乎node.js不支持这种方式的默认参数.上面的最佳方法是什么?
假设我的控制器中有一个通过jQuery AJAX调用调用的方法.我想删除一个用户.当一切顺利时,我返回新内容('ok')并退出方法.
发生错误时该怎么办?我想通过适当的状态代码来表明它,以便调用我的错误回调.为什么状态代码?在这里阅读: 如何使用ASP.NET MVC在jQuery AJAX调用中触发"错误"回调?
但是,该方法不起作用,因为IIS7返回自己的自定义错误消息的消息(错误请求).
除此之外还有另外两个:
错误回调可能如下所示:error:function(request){alert(request.responseText);}
在C++中,为C API回调使用静态成员函数指针是安全/可移植的吗?静态成员函数的ABI是否与C函数相同?
我一直在努力解决这个问题.我是Javascript的新手,并且一直认为我写的代码已经异步运行了.这是一个通用的例子:
我在函数a中运行了一些代码.函数A然后调用函数B,函数B需要将变量返回给A,因此A可以在以后的操作中使用它.看来,当A调用B时,它仍然继续运行自己的代码,而不是等待其返回值被阻塞,并且B不够快,使得A最终到达需要使用返回的点值,我得到一个未定义的变量类型错误.
我解决这个问题的方法是函数A调用函数B然后调用一个函数C,它将执行A将使用返回值执行的后续操作....我有点通过调用序列化我的代码而不是返回......虽然很麻烦......
以下是在实际代码中发生的示例:
function initialize() {
//Geocode Address to obtin Lat and Long coordinates for the starting point of our map
geocoder = new google.maps.Geocoder();
var results = geocode(geocoder);
makeMap(results[0].geometry.location.lat(), results[0].geometry.location.lng());
}
function geocode(geocoder) {
//do geocoding here...
var address = "3630 University Street, Montreal, QC, Canada";
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
return results;
}
else {
alert("Geocode was not successful for the following reason: " + status);
} …Run Code Online (Sandbox Code Playgroud) 在"ViewController.swift"中我创建了这个回调:
func callback(cf:CFNotificationCenter!,
ump:UnsafeMutablePointer<Void>,
cfs:CFString!,
up:UnsafePointer<Void>,
cfd:CFDictionary!) -> Void {
}
Run Code Online (Sandbox Code Playgroud)
使用此观察者:
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
nil,
self.callback,
"myMESSage",
nil,
CFNotificationSuspensionBehavior.DeliverImmediately)
Run Code Online (Sandbox Code Playgroud)
导致此编译器错误:
"AC函数指针只能通过对'func'或文字闭包的引用形成"
我正在尝试使用jQuery通过调用一些自定义API Ajax/$.getJSON.
我正在尝试将自定义值传递给Ajax回调方法,但该值未被传递并且实际上被覆盖.这是我的代码:
var locationType = 3;
var url = 'blah blah blah' + '&locationType=' + locationType;
$("#loading_status").show();
$.getJSON(url, null, function(results, locationType) {
searchResults(results, locationType)
});
Run Code Online (Sandbox Code Playgroud)
locationType使用AJAX调用URL 时BEFORE 的值是3.但是在调用成功返回数据之后,locationType现在的值为success.这是因为回调的方法签名是:
callback(data,textStatus)如果请求成功则执行的回调函数.
如何将一个或多个参数传递给回调方法?
我是一个编程菜鸟,并不太了解回调方法背后的概念.尝试在维基中阅读它并且它超越了我的头脑.有人可以用简单的语言解释一下吗?
我的问题是我不知道如何将回调附加到jquery ui对话框节目.
该节目实际上是一个选项:
$( ".selector" ).dialog({ show: 'slide' });
Run Code Online (Sandbox Code Playgroud)
我希望在幻灯片动画完成后进行回调.我从效果本身看,他们有一个回调:
effect( effect, [options], [speed], [callback] )
Run Code Online (Sandbox Code Playgroud)
但是在对话框中,效果的设置方式却截然不同.我也尝试过:
$( ".selector" ).dialog({ show: 'slide', callback: function() {} });
Run Code Online (Sandbox Code Playgroud)
但它没有用.
建议?
import java.util.concurrent.CountDownLatch;
import quickfix.Initiator;
public class UserSession {
private final CountDownLatch latch = new CountDownLatch(1);
public String await() {
try {
System.out.println("waiting...");
if (latch.await(5, TimeUnit.SECONDS))
System.out.println("released!");
else
System.out.println("timed out");
return secret;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
e.printStackTrace();
}
return null;
}
public void countdown(String s) {
System.out.println("In countdown: "+s+ ". Latch count: "+latch.getCount());
secret = s;
latch.countDown();
System.out.println("Latch count: "+latch.getCount());
}
}
public class LogonHandler extends AbstractHandler {
public void handle(String target, Request baseRequest, …Run Code Online (Sandbox Code Playgroud) callback ×10
asynchronous ×3
javascript ×3
jquery ×2
ajax ×1
asp.net-mvc ×1
c++ ×1
concurrency ×1
default ×1
dialog ×1
effects ×1
java ×1
jquery-ui ×1
methods ×1
node.js ×1
nonblocking ×1
parameters ×1
portability ×1
swift ×1