LoadVars.getBytesLoaded( ) Method | Flash 6 |
check what portion of loading variables has arrived, in bytes |
The number of bytes transferred so far, or undefined if neither load( ) nor sendAndLoad( ) has ever been called on loadVarsObject.
The getBytesLoaded( ) method tells us what portion of loading variables has arrived in the Player. For a new LoadVars object, getBytesLoaded( ) returns undefined. Immediately after load( ) or sendAndLoad( ) is called, getBytesLoaded( ) returns 0. As data arrives, getBytesLoaded( ) returns the number of bytes transferred so far, until all the variables have loaded. From then on, it returns the total number of bytes last transferred, until the next call to load( ) or sendAndLoad( ). To convert bytes to kilobytes (KB), divide by 1024.
Along with getBytesTotal( ), getBytesLoaded( ) is most commonly used to display the progress of variable retrieval, as shown in the following Example.
Example 18-1 displays load progress for variables loaded from a text file, vars.txt, which contains two URL-encoded variables, msg1 and msg2. There are two items of special interest in the code:
We use sendAndLoad( ), instead of load( ), to prevent unwanted caching. See LoadVars.load( ) for details.
We use separate objects for sending and receiving. See LoadVars.sendAndLoad( ) for details.
// Create a text field to display load progress this.createTextField("loadmsg_txt", 1, 200, 200, 100, 20); // Create an object to send variables varSender = new LoadVars(); // Add a dummy property to prevent the browser from using its // cache to retrieve variables varSender.cacheKiller = new Date().getTime(); // Create an object to receive variables varReceiver = new LoadVars(); // Assign a callback function to execute when the variables finish loading varReceiver.onLoad = function () { // Display the loaded variables in the Output window trace("msg1 is " + this.msg1); trace("msg2 is " + this.msg2); // Loading is done, so stop calling our load checker function clearInterval(varPreloader); // Call checkVarStatus() at the end to display the final bytes loaded checkVarStatus(); } // Request variables using sendAndLoad() instead // of load() to prevent the browser from using its cache. varSender.sendAndLoad("http://www.moock.org/temp/vars2.txt", varReceiver, "GET"); // Create an interval to check the variable load status every 100 ms varPreloader = setInterval(checkVarStatus,100); // Function to report variable load status function checkVarStatus () { // Check bytes loaded and convert to KB var kbLoaded = Math.floor(varReceiver.getBytesLoaded()/1024); // Check bytes total and convert to KB var kbTotal = Math.floor(varReceiver.getBytesTotal()/1024); // If the bytes total can't be determined... if (kbTotal = = undefined) { // ...use question marks instead of a total kbTotal = "???"; } // Display the current load status in our text field loadmsg_txt.text = kbLoaded + "/" + kbTotal; }
Macromedia's documentation for Flash MX claims that getBytesLoaded( ) returns undefined when no load operation is in progress. In Flash Player 6, it actually returns the total bytes transferred in the most recent load, even after that load has finished.
LoadVars.getBytesTotal( ), LoadVars.load( ), LoadVars.sendAndLoad( ), setInterval( ), the TextField class