/beyond/webmaster/projects/article.php/3869531/ActionScript-3-Tutorial-Using-Shared-Objects-to-Show-a-Visitors-Browsing-Time.htm ActionScript 3 Tutorial, Using Shared Objects to Show a Visitor's Browsing Time

ActionScript 3 Tutorial, Using Shared Objects to Show a Visitor's Browsing Time

By Community Member FlashKit

Flash developer Paul Flashdaweb from the FlashKit Forumswrote this ActionScript 3 tutorial which will show you how a SharedObject can be used to show the time a user has been visiting a page on your site--even after they have closed the page.

Let's Get Started

For illustration, I came up with an idea to show users the time they spend browsing a particular page--and it works even after they have closed and reopened the page. As an example of what I am referring to, open this demo pageand check out the timer, then close it and open again anytime you want. You will notice that the time will not start again from 0:0, it will continue counting from the values you saw when you previously closed it. Quite nice, isn’t it?

ActionScript Example: Try It Yourself

The above functionality was implemented using Adobe Flash CS3, ActionScript 3 and SharedObject. You may download the source .fla and .as filesto work with them on your own.

The Power of Shared Objects

For this tutorial I assume you already know the basics of AS3 and are quite acquainted with the Flash CS IDE. I created a new document and added a new movie clip called "mainObject". I put one instance of it on the stage and linked it to the ActionScript class SharedObjectReader.as.

In our class, we have a member of shared object:

private var _sharedObj:SharedObject;

In the construction of our class, we create children and start the ActionScript timer-based cycle using setInterval.

public function SharedObjectReader ()

//load and update the correct values

//update the values every second

In the onEverySec method, we bind our _sharedObj to a local instance with the static method getLocal(), put it in a try-catch block to handle the exceptions. TotalTimeAtThisPageis the name of a Shared Object, but you may use anything in there.

_sharedObj = SharedObject.getLocal(”TotalTimeAtThisPage”);
catch (error:Error)
trace(”SharedObject Error:”+error.toString());

When using an initialized shared object, we read the values from it, accessing them via a "data" member

//read the values, if user had already been here, he has them already.
_secs = _sharedObj.data.seconds;
_mins = _sharedObj.data.minutes;

Note, "seconds" and "minutes" are my own variable names. You may use any of them here, but remember that you should use the same names when saving the values.

Now we increase the values, format them and update the text which is displayed. And then we store updated values in the same shared object member _sharedObj.

//store the values
_sharedObj.data.seconds = _secs;
_sharedObj.data.minutes = _mins;

Call flush()to save them to hard disk

//saving the values

Finish the iteration with the close()method calling. It is a must to call this method otherwise it may cause a crash of the Flash IDE or other problems.


Well, that's it! ActionScript SharedObjects are not that tough to learn, but they are very helpful for the developer!

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
Thanks for your registration, follow us on our social networks to keep up-to-date