Persistence in a JavaScript module

拈花ヽ惹草 提交于 2019-12-11 13:37:17

问题


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.

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


回答1:


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) { /* ... */}


来源:https://stackoverflow.com/questions/27364000/persistence-in-a-javascript-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!