Please note that ExpatTech is close for the Christmas and New Year holidays. We will re-open on 2nd January 2024.

ExpatTech Techblog

Peter Todd 2010.01.11. 14:24

Flash Cookbook - Tricks - SharedObject with expiration

So to say SharedObject is flash's cookie. To reach it simply do:

var _so:SharedObject = SharedObject.getLocal(name);

to reach key-value pairs use the data property

_so.data[key] for both read and write.

SharedObject stores an object, so we can use it for e.g. setting expiration times. To get the actual time use the Date classes getTime() method:

var time:Number = (new Date).getTime();

Knowing this we can easily put it together:

_so.data[key] = {value: value, time: time, expiry: 60000};

checking if our record has expired (in expiry: 60000 millisec = 60 sec = 1 minute):

var temp:Object = _so.data[key];
var expired:Boolean = temp.expiry == '-1' ? false : (((new Date()).getTime() - temp.time) > temp.expiry ? true : false);

Default value for expiry is -1 when it's not set for expiry.

There you go, fast and clean and useful for local operations.