持久化一个JavaScript模块 [英] Persistence in a JavaScript module

查看:186
本文介绍了持久化一个JavaScript模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写旋转在SharePoint Web部件显示横幅图片一个JavaScript模块。我想依靠的SharePoint的API尽可能少,并保持尽可能包含驱动Web部件中的JavaScript模块中可能的自我。我想跟踪的最后一次我的code在列表中前进到下一个图像,这是什么形象是。该列表将本身将在SharePoint通过REST API坚持和可用的。

I need to write a JavaScript module that rotates banner images displayed in a SharePoint web part. I would like to rely on SharePoint APIs as little as possible, and keep as much as possible self contained in the JavaScript module that drives the web part. I would like to keep track of the last time my code advanced to the next image in a list, and what that image was. The list will itself will be persisted in SharePoint and available through the REST API.

所以,在现代浏览器的JavaScript运行环境,是否有当地持续的小记录我上面描述的任何手段?

So, in the modern browser JavaScript runtime environment, is there any means of locally persisting the small record I describe above?

推荐答案

有几种方法。但是,对于这样的事情,我会去与 localStorage的。例如:

There are a few ways. But, for something like this, I would go with localStorage. For instance:

localStorage.setItem('lastSlide', JSON.stringify({
  time: +new Date,
  src: 'http://lorempixel.com/100/100'
}));

// Then, to retrieve:
var lastSlide;
try {
  lastSlide = JSON.parse(localStorage.getItem('lastSlide'));
} catch(e) { /* ... */}

这篇关于持久化一个JavaScript模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆