nutman
nutman

Reputation: 563

Flash sharedobject flush help

On my site I have a flash banner that saves its frame position when the user changes pages so that it continues to play rather than restart every time a user navigates around the site. This was implemented using sharedobject. It works great except for one thing: the cookie lasts too long. Even when I come back to the site later on in the day it still reloads my last position. I want to flush the sharedobject when the user navigates away from the domain name but I'm unsure how to implement it. Could someone help please!

Ok thanks for your response Lars. Here's what I got:

var mySharedObject:SharedObject = SharedObject.getLocal("displayCookie");

var expiredate = mySharedObject.data.expires;

var timeobject = new Date();

var timestamp = timeobject.getTime();

if (expiredate<=timestamp && expiredate != null) {
for (var i in mySharedObject.data) {
delete mySharedObject.data[i];
}
mySharedObject.flush();
} else if (expiredate == null) {

var oneday = 100;

var expiresIn = 1;

var expiretimestamp = timestamp+expiresIn*oneday;

mySharedObject.data.expires = expiretimestamp;

mySharedObject.flush();
}
if (mySharedObject.data.introcheck != 0) {
mySharedObject.data.introcheck = 0;
mySharedObject.flush();
gotoAndPlay(1);
} else {

addEventListener(Event.ENTER_FRAME, checkLoadedFrames);

function checkLoadedFrames(e:Event):void {
   if(this.framesLoaded == this.totalFrames) {
        removeEventListener(Event.ENTER_FRAME, checkLoadedFrames);
        checkSharedObject();
   }
}

function checkSharedObject():void {
    if(mySharedObject.data.currentFrame){
       gotoAndPlay(mySharedObject.data.currentFrame); 
    }
    addEventListener(Event.ENTER_FRAME, saveCurrentFrame);
}

function saveCurrentFrame(e:Event):void {
   mySharedObject.data.currentFrame = this.currentFrame;
}
}

I changed the oneday var to 100ms to check if it works but it doesn't. What am I doing wrong?

Upvotes: 0

Views: 509

Answers (1)

Lars Bl&#229;sj&#246;
Lars Bl&#229;sj&#246;

Reputation: 6127

A SharedObject is not a cookie, it never expires.

Assuming this question is about the same case as this previous question, I guess you could either switch from a SharedObject to a browser cookie (that has mechanisms for expiring), and read/write to it using ExternalInterface, or you could save a timestamp on the SharedObject, and ignore it/reset it if it is too old.

Edit:

I think this could work, basically removing some of the if/else logic of your code and instead always do these steps:

  1. Reset/clear the SharedObject if it has expired.
  2. Save a new timestamp, for the next run.
  3. Do the original check of mySharedObject.data.currentFrame (if the SharedObject was reset in step 1, it will be like a first run).

-

var mySharedObject:SharedObject = SharedObject.getLocal("displayCookie");

var expiredate = mySharedObject.data.expires;

var timeobject = new Date();

var timestamp = timeobject.getTime();

// Reset/clear the SO if it has expired
if (expiredate != null && expiredate <= timestamp)
{
    for (var i in mySharedObject.data)
    {
        delete mySharedObject.data[i];
    }
    mySharedObject.flush();
}

// Make a new timestamp, for comparing the next run to this one

var oneday = 10000; // Test value, 10 seconds   

var expiresIn = 1;

var expiretimestamp = timestamp + expiresIn * oneday;

mySharedObject.data.expires = expiretimestamp;

mySharedObject.flush();

// Do the original check
addEventListener(Event.ENTER_FRAME, checkLoadedFrames);

function checkLoadedFrames(e:Event):void
{
    if (this.framesLoaded == this.totalFrames)
    {
        removeEventListener(Event.ENTER_FRAME, checkLoadedFrames);
        checkSharedObject();
    }
}

function checkSharedObject():void
{
    if (mySharedObject.data.currentFrame)
    {
        gotoAndPlay(mySharedObject.data.currentFrame);
    }
    addEventListener(Event.ENTER_FRAME, saveCurrentFrame);
}

function saveCurrentFrame(e:Event):void
{
    mySharedObject.data.currentFrame = this.currentFrame;
}

Upvotes: 2

Related Questions