[英]How to convert asynchronous call to synchronous in javascript?
I am using javascript in client side and node.js in server side, Actually i have tried to make a remote validation for input fields, result must be boolean and it should be based on the server call, My code as follows, 我在客户端使用javascript,在服务器端使用node.js,实际上我已尝试对输入字段进行远程验证,结果必须为布尔值,并且应基于服务器调用,我的代码如下,
jQuery.validator.addMethod("remoteValidation", function(value, element) {
var accountName = $("#accountName").val();
var accountType = $("#accountType").val();
var valid = this.Validation(accountType,accountName);
//returning undefined becoz return statement executing before server side execution
return valid;
}, "Entered email or username already exists");
this.validation = function(accountType,accountName,cb){
var valid =null;
ss.rpc('AccountsManagement.userNameChecker',accountType,accountName,function(res) {
// res will return boolean
valid = res;
});
//valid is null becoz this line is executing before valid = res line.
return valid;
};
my server side code: 我的服务器端代码:
services.imAccountsService.getAllImAccounts(accountType,accountName,
function(err,result){
if(err){
return;
}
if(result == null){
res(true);
}
else{
res(false);
}
});
My problem is async execution, how to make code synschronous in this situation.. 我的问题是异步执行,在这种情况下如何使代码同步。
Added function in the jquery validation method. 在jquery验证方法中添加了功能。
if(param.type === "rpc"){
var args = param.data.remoteArgs();
ss.rpc(param.url, args, function(response) {
validator.settings.messages[element.name].remote = previous.originalMessage;
var valid = response === true;
if ( valid ) {
var submitted = validator.formSubmitted;
validator.prepareElement(element);
validator.formSubmitted = submitted;
validator.successList.push(element);
validator.showErrors();
} else {
var errors = {};
var message = response || validator.defaultMessage( element, "remote" );
errors[element.name] = previous.message = $.isFunction(message) ? message(value) : message;
validator.showErrors(errors);
}
previous.valid = valid;
validator.stopRequest(element, valid);
});
}
It works fine ....thanks for all... 一切正常...谢谢大家...
You will have to wait till the value of res is available. 您将不得不等到res的值可用。 See if the below code works.
查看以下代码是否有效。
jQuery.validator.addMethod("remoteValidation", function(value, element) {
var accountName = $("#accountName").val();
var accountType = $("#accountType").val();
var valid = this.Validation(accountType,accountName);
//returning undefined becoz return statement executing before server side execution
return valid;
}, "Entered email or username already exists");
this.validation = function(accountType,accountName,cb){
var valid =null;
ss.rpc('AccountsManagement.userNameChecker',accountType,accountName,function(res) {
// res will return boolean
while(res <> null)
{
window.setInterval(function(){waitFunc()}, 1000);
}
valid = res;
});
//valid is null becoz this line is executing before valid = res line.
return valid;
};
function waitFunc()
{
//do nothing
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.