我正在使用 jquery.mobile-1.1.1.min.js 和 cordova-2.0.0.js (phonegap)。
在我的 $.ajax 调用设置中,我将参数设置如下:
success: onFetchOK, // saves everything to the database
complete: function(jqXHR, textStatus) {
// show everything in the database
showAllHeaders();
},
error: function(xhr, textStatus, errorThrown) {
alert("onFetchFailure: There was an error retrieving messages: \n"
+"xhr: " + xhr.statusText
+"\nstatus: " +textStatus
+"\nerror: " +errorThrown.message);
$('#fetch-result').html(textStatus);
},
onFetchOK 函数从服务器获取 18 个消息头,每个消息头都有一个主题和一个日期/时间,这些都使用 phonegap webdb 存储 API 存储在 webdb 中。
如果我删除并重新创建消息表并重新运行应用程序,那么对于我收到的 18 条消息中的每条消息都会调用一次 complete: 函数,因此会显示 342 个消息头(18x18+18)。
除了这个之外,我的代码中没有其他地方完整:showAllHeaders(); 被叫。
我在 showAllHeaders() 中放置了一个警报,该警报被触发了 18 次。
为了进一步缩小范围,我在我的成功中放置了一个循环计数器: onFetchOK() 像这样:
var loopCount = 0;
// process each message
$.each(json.messages, function(index, msg)
{
// save each alert before displaying it
webdb.addAlert(msg.msg_id, msg.title, msg.when);
loopCount++;
});
alert("loopCount = " +loopCount);
调用此警报时,它会显示“loopCount = 18”,并且仅调用一次。
如果我退出应用程序,重新加载它并且没有从服务器中提取新消息,则会显示 18 个消息头,这正是它应该的样子。
所以,我的 complete: function() 似乎被重复调用......一次应该,然后再调用 18 次。
有人知道为什么会这样吗?