[英]How does jQuery do async:false in its $.ajax method?
I have a similar question here , but I thought I'd ask it a different way to cast a wider net. 我在这里也有类似的问题 ,但我想我会问它以不同的方式来扩大范围。 I haven't come across a workable solution yet (that I know of).
我还没有遇到可行的解决方案(据我所知)。
I'd like for XCode to issue a JavaScript command and get a return value back from an executeSql callback. 我想让XCode发出JavaScript命令并从executeSql回调中获取返回值。
From the research that I've been reading, I can't issue a synchronous executeSql command. 根据我一直在阅读的研究,我无法发出同步executeSql命令。 The closest I came was trying to Spin Lock until I got the callback.
我最接近的是尝试旋转锁,直到获得回调。 But that hasn't worked yet either.
但这还没有奏效。 Maybe my spinning isn't giving the callback chance to come back (See code below).
也许我的旋转没有给回调机会返回(请参见下面的代码)。
Q: How can jQuery have an async=false argument when it comes to Ajax? 问:关于Ajax,jQuery如何具有async = false参数? Is there something different about XHR than there is about the executeSql command?
XHR与executeSql命令有什么不同吗?
Here is my proof-of-concept so far: (Please don't laugh) 到目前为止,这是我的概念证明:(请不要笑)
// First define any dom elements that are referenced more than once.
var dom = {};
dom.TestID = $('#TestID'); // <input id="TestID">
dom.msg = $('#msg'); // <div id="msg"></div>
window.dbo = openDatabase('POC','1.0','Proof-Of-Concept', 1024*1024); // 1MB
!function($, window, undefined) {
var Variables = {}; // Variables that are to be passed from one function to another.
Variables.Ready = new $.Deferred();
Variables.DropTableDeferred = new $.Deferred();
Variables.CreateTableDeferred = new $.Deferred();
window.dbo.transaction(function(myTrans) {
myTrans.executeSql(
'drop table Test;',
[],
Variables.DropTableDeferred.resolve()
// ,WebSqlError
);
});
$.when(Variables.DropTableDeferred).done(function() {
window.dbo.transaction(function(myTrans) {
myTrans.executeSql(
'CREATE TABLE IF NOT EXISTS Test'
+ '(TestID Integer NOT NULL PRIMARY KEY'
+ ',TestSort Int'
+ ');',
[],
Variables.CreateTableDeferred.resolve(),
WebSqlError
);
});
});
$.when(Variables.CreateTableDeferred).done(function() {
for (var i=0;i < 10;i++) {
myFunction(i);
};
Variables.Ready.resolve();
function myFunction(i) {
window.dbo.transaction(function(myTrans) {
myTrans.executeSql(
'INSERT INTO Test(TestID,TestSort) VALUES(?,?)',
[
i
,i+100000
]
,function() {}
,WebSqlError
)
});
};
});
$.when(Variables.Ready).done(function() {
$('#Save').removeAttr('disabled');
});
}(jQuery, window);
!function($, window, undefined) {
var Variables = {};
$(document).on('click','#Save',function() {
var local = {};
local.result = barcode.Scan(dom.TestID.val());
console.log(local.result);
});
var mySuccess = function(transaction, argument) {
var local = {};
for (local.i=0; local.i < argument.rows.length; local.i++) {
local.qry = argument.rows.item(local.i);
Variables.result = local.qry.TestSort;
}
Variables.Return = true;
};
var myError = function(transaction, argument) {
dom.msg.text(argument.message);
Variables.result = '';
Variables.Return = true;
}
var barcode = {};
barcode.Scan = function(argument) {
var local = {};
Variables.result = '';
Variables.Return = false;
window.dbo.transaction(function(myTrans) {
myTrans.executeSql(
'SELECT * FROM Test WHERE TestID=?'
,[argument]
,mySuccess
,myError
)
});
for (local.I = 0;local.I < 3; local.I++) { // Try a bunch of times.
if (Variables.Return) break; // Gets set in mySuccess and myError
SpinLock(250);
}
return Variables.result;
}
var SpinLock = function(milliseconds) {
var local = {};
local.StartTime = Date.now();
do {
} while (Date.now() < local.StartTime + milliseconds);
}
function WebSqlError(tx,result) {
if (dom.msg.text()) {
dom.msg.append('<br>');
}
dom.msg.append(result.message);
}
}(jQuery, window);
Is there something different about XHR than there is about the executeSql command?
XHR与executeSql命令有什么不同吗?
Kind of. 的种类。
How can jQuery have an async=false argument when it comes to Ajax?
关于Ajax,jQuery如何具有async = false参数?
Ajax, or rather XMLHttpRequest
, isn't strictly limited to being asynchronous -- though, as the original acronym suggested, it is preferred. Ajax或
XMLHttpRequest
并不严格限于异步-尽管,正如最初的缩写所建议的那样,它是首选。
jQuery.ajax()
's async
option is tied to the boolean async
argument of xhr.open()
: jQuery.ajax()
的async
选项绑定到xhr.open()
的boolean async
参数:
void open(
DOMString method,
DOMString url,
optional boolean async, // <---
optional DOMString user,
optional DOMString password
);
The Web SQL Database spec does also define a Synchronous database API . Web SQL数据库规范还定义了一个同步数据库API 。 However, it's only available to implementations of the
WorkerUtils
interface, defined primarily for Web Workers : 但是,它仅适用于主要为Web Workers定义的
WorkerUtils
接口的实现:
window.dbo = openDatabaseSync('POC','1.0','Proof-Of-Concept', 1024*1024);
var results;
window.dbo.transaction(function (trans) {
results = trans.executeSql('...');
});
If the environment running the script hasn't implemented this interface, then you're stuck with the asynchronous API and return
ing the result will not be feasible. 如果运行脚本的环境尚未实现此接口,则您将陷入异步API的困境,并且
return
结果将不可行。 You can't force blocking/waiting of asynchronous tasks for the reason you suspected: 由于以下原因,您不能强制阻止/等待异步任务:
Maybe my spinning isn't giving the callback chance to come back (See code below).
也许我的旋转没有给回调机会返回(请参见下面的代码)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.