我需要在列表之间复制项目(向每个项目添加更多的东西,所以不能使用流),如果源列表大于100个项,脚本将在查询时终止到源列表中(第一件事是按一下按钮),大约要执行4到10次。
我使用承诺来确保一切按顺序进行,但这似乎不是问题所在,而是查询失败,这是承诺结构的第一步。
它不是查询中的行限制,因为它被设置为1000,行数约为200。
下面是一个示例代码,它可以很好地处理少量的项,但是当两个列表都包含大约100个项时,需要多次运行:
<head>
<script
src="https://code.jquery.com/jquery-3.3.1.js"
integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
crossorigin="anonymous"></script>
<script>
function update() {
console.log("1")
var dfd = $.Deferred(function () {
var updateBtnCLientContextForSourceList =
new SP.ClientContext.get_current();
var getSourceList = updateBtnCLientContextForSourceList
.get_web()
.get_lists()
.getByTitle("SourceList");
var camlQueryForSourceList = new SP.CamlQuery();
camlQueryForSourceList.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' +
'<Value Type=\'Number\'>1</Value></Geq></Where></Query><RowLimit>4000</RowLimit></View>');
var SourceListtStore = getSourceList.getItems(camlQueryForSourceList);
updateBtnCLientContextForSourceList.load(SourceListtStore);
updateBtnCLientContextForSourceList.executeQueryAsync(
function () {
dfd.resolve(SourceListtStore);
},
function (sender, args) {
dfd.reject(args);
}
);
});
console.log("2")
return dfd.promise();
}
acadcount = 0;
function check() {
update()
.then(
function (SourceListtStore) {
console.log("3")
acadcount = SourceListtStore.get_count()
console.log("4")
var dfd = $.Deferred(
function () {
var updateBtnCLientContextForWeeksAllocated =
SP.ClientContext.get_current();
var getWeeksAllocated =
updateBtnCLientContextForWeeksAllocated
.get_web()
.get_lists()
.getByTitle("Weeks Allocated");
var camlQueryForWeeksAllocated = new SP.CamlQuery();
camlQueryForWeeksAllocated.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' +
'<Value Type=\'Number\'>1</Value></Geq></Where></Query><RowLimit>1000</RowLimit></View>');
var weeksAllocatedListStore =
getWeeksAllocated.getItems(camlQueryForWeeksAllocated);
updateBtnCLientContextForWeeksAllocated.load(weeksAllocatedListStore);
updateBtnCLientContextForWeeksAllocated.executeQueryAsync(
function () {
dfd.resolve(weeksAllocatedListStore);
},
function (sender, args) {
dfd.reject(args);
}
);
}
);
console.log("5")
return dfd.promise()
}
).then(
function (waListStore) {
console.log("6")
if (waListStore.get_count() === acadcount) {
console.log("7")
alert("All items have been copied")
}
else {
console.log("8")
alert("Please try again")
}
}
);
}
</script>
</head>
<body>
<button onClick="check()">Check</button>
</body>
源列表:2项,周分配:3项
来源列表:109个项目,周分配:1个项目
来源列表:109个项目,周分配:100个项目
那么,我是否正确地假设,当脚本失败时,promise.return会在.resolve完成之前运行?如果是这样的话,我怎样才能避开这一切呢?
发布于 2018-06-05 07:29:18
在JS有机会使用大量的项完全执行之前,该页面正在重新加载,禁用了按钮单击上的刷新功能,现在可以正常工作了。
https://stackoverflow.com/questions/50652099
复制相似问题