作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的应用程序中,我有一个同步函数,其中我在开始和结束时获取两个时间戳,以获取同步时花费的时间。
我想将此变量保存到本地存储中。
之后,我需要将来自函数的变量与来自函数的变量进行比较,并获取它们的平均值。
我知道存储是键值类型,但我在完成这项工作时仍然遇到问题。该功能发布在下面。感谢您提供的一切可能的帮助。
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/
我正在使用 sapui5 创建一个表。我想知道我是否可以将升序排序和降序排序的默认图标更改为我自己的图标? 补充问题:有没有一种方法可以在标题中显示我的自定义图标而无需先点击它? 最佳答案 只需在 W
我如何在 SapUI5 中构建一个 XML View 来迭代 JSONModel 中的所有元素? 到目前为止,我有一个 Controller : sap.ui.define([ "sap/ui
我是一名优秀的程序员,十分优秀!