這是代碼的一部分(代碼不是我的,我不明白它是如何作業的,也許它被稱為 Promise,但我不確定)。
m = {
mounted: function() {
var e = this;
this.$bus.on("buff-event", (function(t) {
e.buff(t)
}))
},
methods: {
buff: function(e) {
var t = this;
this.$bus.emit("bfceebecbb-change", "Motivating...");
var n = '[{"__class__":"ServerRequest","requestData":[],"requestClass":"OtherPlayerService","requestMethod":"' e '","requestId":%%requestId%%}]';
this.requestFaker.fetch(n).then((function(i) {
i = (i = i.filter((function(t) {
return t.requestMethod == e
}))[0]).responseData.filter((function(e) {
return void 0 === e.next_interaction_in && !e.is_self && (void 0 === e.accepted || 1 == e.accepted)
})), n = [], _.forEach(i, (function(e) {
n.push('{"__class__":"ServerRequest","requestData":[' e.player_id '],"requestClass":"OtherPlayerService","requestMethod":"polivateRandomBuilding","requestId":%%requestId%%}')
})), n.length ? (n = "[" n.join(",") "]", t.requestFaker.fetch(n).then((function() {
t.$bus.emit("bfceebecbb-change", "Motivation success")
}))) : t.$bus.emit("bfceebecbb-change", "Nobody to motivate")
}))
}
}
},
該代碼收集用戶資料,存盤到陣列中,然后將其傳遞給服務器。所以它在一個請求中發送所有用戶的大陣列(它在這部分將所有條目推送到陣列n.push('{"__class__":"ServerRequest"...)。
我想要實作的是為每個用戶一個一個地發送請求,并有一些延遲(例如請求之間的延遲為 1 秒)。
我嘗試了很多方法來實作它,但都沒有成功,我對這種編程語言缺乏了解。
我嘗試以setTimeout不同的方式使用函式,但一直都沒有成功:
methods: {
buff: function(e) {
var t = this;
this.$bus.emit("bfceebecbb-change", "Motivating...");
var n = '[{"__class__":"ServerRequest","requestData":[],"requestClass":"OtherPlayerService","requestMethod":"' e '","requestId":%%requestId%%}]';
this.requestFaker.fetch(n).then((function(i) {
i = (i = i.filter((function(t) {
return t.requestMethod == e
}))[0]).responseData.filter((function(e) {
return void 0 === e.next_interaction_in && !e.is_self && (void 0 === e.accepted || 1 == e.accepted)
})), n = [], _.forEach(i, (function(e) {
n.push('{"__class__":"ServerRequest","requestData":[' e.player_id '],"requestClass":"OtherPlayerService","requestMethod":"polivateRandomBuilding","requestId":%%requestId%%}')
})), n.length ?
setTimeout((function() {
(setTimeout((function() {n = "[" n.join(",") "]"}), 1000) , t.requestFaker.fetch(n).then((function() {
t.$bus.emit("bfceebecbb-change", "Motivation success")
})))}), 1000) : t.$bus.emit("bfceebecbb-change", "Nobody to motivate")
}))
}
}
也試過這樣:
methods: {
buff: function(e) {
var t = this;
this.$bus.emit("bfceebecbb-change", "Motivating...");
var n = '[{"__class__":"ServerRequest","requestData":[],"requestClass":"OtherPlayerService","requestMethod":"' e '","requestId":%%requestId%%}]';
this.requestFaker.fetch(n).then((function(i) {
i = (i = i.filter((function(t) {
return t.requestMethod == e
}))[0]).responseData.filter((function(e) {
return void 0 === e.next_interaction_in && !e.is_self && (void 0 === e.accepted || 1 == e.accepted)
})), n = [], _.forEach(i, (function(e) {
n.push('{"__class__":"ServerRequest","requestData":[' e.player_id '],"requestClass":"OtherPlayerService","requestMethod":"polivateRandomBuilding","requestId":%%requestId%%}')
})), n.length ?
setTimeout((function() {
(n = "[" n.join(",") "]", t.requestFaker.fetch(n).then((function() {
t.$bus.emit("bfceebecbb-change", "Motivation success")
})))}), 1000) : t.$bus.emit("bfceebecbb-change", "Nobody to motivate")
}))
}
}
更新
根據評論詢問是什么%%requestId%%- 我找到了這部分:
{
key: "fetch",
value: function(e) {
function t(t) {
return e.apply(this, arguments)
}
return t.toString = function() {
return e.toString()
}, t
}((function(e) {
for (var t = e;
(t = e.replace("%%requestId%%", this.requestId)) !== e;) e = t, this.incRequestId();
return fetch(gameVars.gatewayUrl, this.getHeadForFetchQuery(e)).then((function(e) {
return e.json()
}))
}))
}
uj5u.com熱心網友回復:
試試這個
一般來說,forEach應該用for..of回圈替換,使用async/await輔助函式會延遲請求(await delaySome(1000);部分)
它應該以相同的格式發送資料,如果它在服務器上失敗,那可能就是它需要調整的地方..或其他任何地方..
m = {
mounted: function() {
var e = this;
this.$bus.on("buff-event", function(t) {
e.buff(t);
});
},
methods: {
buff: function(e) {
var t = this;
function delaySome(delay) {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, delay);
});
}
this.$bus.emit("bfceebecbb-change", "Motivating...");
var n = '[{"__class__":"ServerRequest","requestData":[],"requestClass":"OtherPlayerService","requestMethod":"' e '","requestId":%%requestId%%}]';
this.requestFaker.fetch(n).then(async function(i) {
(i = (i = i.filter(function(t) {
return t.requestMethod == e;
})[0]).responseData.filter(function(e) {
return void 0 === e.next_interaction_in && !e.is_self && (void 0 === e.accepted || 1 == e.accepted);
})),
(n = []);
if (i.length) {
for (const e of i) {
await delaySome(1000);
t.requestFaker.fetch('[{"__class__":"ServerRequest","requestData":[' e.player_id '],"requestClass":"OtherPlayerService","requestMethod":"polivateRandomBuilding","requestId":%%requestId%%}]').then(function() {
t.$bus.emit("bfceebecbb-change", "Motivation success");
})
}
} else {
t.$bus.emit("bfceebecbb-change", "Nobody to motivate")
}
});
},
},
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/469096.html
標籤:javascript 节点.js 数组 承诺
上一篇:二值影像中所有可能的組合
