gpt4 book ai didi

javascript - SAPUI5 jQuery.sap.storage变量保存

转载 作者:行者123 更新时间:2023-12-03 08:09:48 29 4
gpt4 key购买 nike

在我的应用程序中,我有一个同步函数,其中我在开始和结束时获取两个时间戳,以获取同步时花费的时间。

我想将此变量保存到本地存储中。

之后,我需要将来自函数的变量与来自函数的变量进行比较,并获取它们的平均值。

我知道存储是键值类型,但我在完成这项工作时仍然遇到问题。该功能发布在下面。感谢您提供的一切可能的帮助。

handleSyncPress: function() {

new Date().getTime();
var syncStart = Math.floor(Date.now() / 1000);

var that = this;
var fUpdateBindings = function() {
that.getView().getModel().refresh(true);
}
test.mp.Offline.sync(fUpdateBindings);

new Date().getTime();
var syncEnd = Math.floor(Date.now() / 1000);

var syncTime = syncEnd - syncStart;
this._oStorage = jQuery.sap.storage(jQuery.sap.storage.Type.local);
this._oMyData = this._oStorage.get(syncTime);
this._oStorage.put(syncTime, this._oMyData);
}

如您所见,我至少已经开始存储初始化。

最佳答案

正如我在其他问题的评论中所说,存储就像存储键值对的字典。

key 是您稍后用来访问您的值的标识符。

该值可以是任何内容:数字、字符串、 bool 值、数组、对象,应有尽有。

我认为最好的解决方案是将所有同步时间存储在一个值中(即同步时间数组)。

handleSyncPress: function() {
// get current timestamp
var syncStart = Date.now();

// do stuff
var fUpdateBindings = function() {
that.getView().getModel().refresh(true);
}

test.mp.Offline.sync(fUpdateBindings);

// get another timestamp
var syncEnd = Date.now();
// diff between the timestamps is the sync time (in milliseconds)
var syncTimeInMilliseconds = syncEnd - syncStart;

this._oStorage = jQuery.sap.storage(jQuery.sap.storage.Type.local);
// load value for the key "syncTimes"
var aSyncTimes = this._oStorage.get("syncTimes");
aSyncTimes = JSON.parse(aSyncTimes); // may not be needed
// if this is the first time you access the key, initialize the value
if (aSyncTimes === null) {
aSyncTimes = [];
}
// append your new sync time
aSyncTimes.push(syncTimeInMilliseconds);
// store your sync time array
aSyncTimes = JSON.stringify(aSyncTimes); // may not be needed
this._oStorage.put("syncTimes", aSyncTimes);

// hopefully you already know how to calculate the avg value from an array of integers
// if not: avg = sum / length
}
<小时/>

编辑:根据 API,仅支持字符串作为值。我尝试了其他类型,它们也有效,但(反)序列化数据可能是最安全的。我更新了代码示例。

关于javascript - SAPUI5 jQuery.sap.storage变量保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34197028/

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