gpt4 book ai didi

javascript - jQuery 如何执行 async :false in its $. ajax 方法?

转载 作者:行者123 更新时间:2023-11-28 02:23:05 25 4
gpt4 key购买 nike

我有一个similar question here ,但我想我应该以不同的方式提出问题,以撒下更广泛的网。我还没有找到可行的解决方案(据我所知)。

我希望 XCode 发出 JavaScript 命令并从executeSql 回调获取返回值。

根据我一直在阅读的研究,我无法发出同步executeSql 命令。我最接近的是尝试旋转锁定,直到收到回调。但这也还没有奏效。也许我的旋转没有给回调机会回来(参见下面的代码)。

问:当涉及到 Ajax 时,jQuery 如何具有 async=false 参数? XHR 与executeSql 命令有什么不同吗?

这是我迄今为止的概念验证:(请不要笑)

// 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?

有点。

How can jQuery have an async=false argument when it comes to Ajax?

Ajax,或者更确切地说XMLHttpRequest ,并不严格限于异步——不过,如 original acronym建议,首选。

jQuery.ajax()async 选项与 xhr.open()boolean async 参数相关联。 :

void open(
DOMString method,
DOMString url,
optional boolean async, // <---
optional DOMString user,
optional DOMString password
);

Web SQL Database规范还定义了 Synchronous database API 。然而,它是only available到主要为 Web Workers 定义的 WorkerUtils 接口(interface)的实现:

window.dbo = openDatabaseSync('POC','1.0','Proof-Of-Concept', 1024*1024);

var results;
window.dbo.transaction(function (trans) {
results = trans.executeSql('...');
});

如果运行脚本的环境尚未实现此接口(interface),那么您将陷入异步 API 的困境,并且返回结果将不可行。您不能因为您怀疑的原因强制阻塞/等待异步任务:

Maybe my spinning isn't giving the callback chance to come back (See code below).

关于javascript - jQuery 如何执行 async :false in its $. ajax 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15353737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com