views:

24

answers:

2

Hi Forum

This is someting weird...

I have a Flash-file that loads external SWFs. There I use LoadVars to get external data from a server. When the data has arrived, I'm using setInterval to call a function that fades-in the content.

So far so good.

But when I switch menus (a.k.a. unload one movie to load another) before all the data has arrived, Flash hangs for like 15 seconds and then shows me the infamous message «A script in this movie is slowing down Flash - would you like to abort it?» (roughly translated).

I attached an «onUnload»-function to the SWF that causes the major problems, where I use clearInterval to get rid of the interval and re-instantiate the LoadVars-Variable.

Nothing helps...

Any ideas?

as per request - here's the source of the SWF causing the trouble:

var myTimer = null;
var server_send:LoadVars = new LoadVars();

this.onUnload = function () {
    clearInterval(myTimer);
    server_send = new LoadVars();
    trace("stopping gewerbe");
};

var myself = scrollContent;

import JSON;    // external Class for parsing JSON

var sRes:LoadVars = new LoadVars();

function fadeIn(which){
    which._alpha += 3;

    if(which._alpha >= 100){
        clearInterval(myTimer);
    }
}

sRes.onLoad = function(success:Boolean){

    if (success){

        var o:Object = JSON.parse(sRes.res);
        var s:String = JSON.stringify(o); 

        //trace(sRes.res);

        var y = 0;

        for(item in o){

            actCol = 0;

            myself.attachMovie("row", "entry" + item, myself.getNextHighestDepth(), {_x: 0, _y:y});

            var tr = myself["entry" + item];

            tr.cTitle = o[item]["title"];
            tr.cBild = o[item]["image"];
            tr.cText = o[item]["text"];
            y += 65;

        }

        myTimer = setInterval(fadeIn, 0.1, myself);

        _root.myIntervals.push(myTimer);

    }
    else
    {
        trace("no connection...");
    }

}

myself._alpha = 0;

var vars = Array("id");
var values = Array("3");

server_send.load('void.txt');

for(var i=0;i<vars.length;i++)
{
    server_send[vars[i]] = escape(values[i]);
}

server_send.sendAndLoad(_global.server, sRes, "POST");

stop();
A: 

Update

First off I'd suggest you drop the use of setInterval, they're particularly problematic. Instead use the Timer class, which is far more straightforward to manage and use.

The setInterval inside the sRes.onLoad function is probably your issue though. The interval should be in milliseconds, so your 0.1 should probably be 100 (for 1/10th second), and the clearInterval calls in fadeIn and unLoad might have myInterval out of scope, check these with a trace to make sure they are clearing as expected.

Code

Ok, I've re-ordered the code to make it a bit more readable... However, it is still in very bad shape. I'm marking things out that I'd need to discuss with you further... (see below additional comments in the code.)

stop();

// Moved import to the top.
import JSON;

// Grouping var declarations.
var myself = scrollContent;
myself._alpha = 0;

var values = Array("3");
var myTimer = null;
var sRes:LoadVars = new LoadVars();
var server_send:LoadVars = new LoadVars();
var vars = Array("id");    

// why are we looping through an array of one value?
for(var i=0; i<vars.length; i++)
{
    server_send[vars[i]] = escape(values[i]);
}

server_send.load('void.txt'); // what is this for?

server_send.sendAndLoad(_global.server, sRes, "POST"); // where do we define _global.server

this.onUnload = function () {
    clearInterval(myTimer); // is myTimer in scope here?
    server_send = new LoadVars();
    trace("stopping gewerbe");
};    

function fadeIn(which){
    which._alpha += 3;
    if(which._alpha >= 100){
        clearInterval(myTimer); // is myTimer in scope here?
    }
}

sRes.onLoad = function(success:Boolean)
{
    if (success)
    {
        var o:Object = JSON.parse(sRes.res);
        var s:String = JSON.stringify(o); 
        var y = 0;

        for(item in o)
        {
            actCol = 0;
            myself.attachMovie("row", "entry" + item, myself.getNextHighestDepth(), {_x: 0, _y:y});
            var tr = myself["entry" + item];
            tr.cTitle = o[item]["title"];
            tr.cBild = o[item]["image"];
            tr.cText = o[item]["text"];
            y += 65;
        }

        myTimer = setInterval(fadeIn, 0.1, myself); // setInterval uses milliseconds 0.1 is almost certainly the problem. 
                                                    // if you want 1/10th of a second, specify 100 as the interval.
        _root.myIntervals.push(myTimer); // We never see _root.myIntervals again, what's this for? 
                                         // The fadeIn and unLoad methods could use this to 
                                         // guarantee that they clear the right interval.
    }
    else
    {
        trace("no connection...");
    }
}
slomojo
thanks - I added some code now. My suspicion is the same, that there's some sort of loop somewhere - but for the life of me, I can't find any problem...
Swissdude
Thanks a lot slomojo - I know the coding style is awful - I'll refactor the whole thing according to your suggestions. As you can see below, seems like I found the bug...
Swissdude
A: 

Thanks for the help - seems like I found the quirk...

The sendAndLoad-response was still called, even when the movie was unloaded, causing a loop, it seems.

What I did is:

added a variable at the top of the script called «doParse» and set it to true

var doParse = true;

Then in the unload-function, I set this var to false:

this.onUnload = function () {
doParse = false;
...
}

and in the event that gets called when the data arrives, I added this:

if(success && doParse == true){
...
}

This helped and the message disappeared.

Swissdude
You didn't find the problem, it's likely your setIntervals not getting cleared properly (due to scope problems) and the interval time set to 0.1 (should be in milliseconds, i.e. 100 for 1/10th second.) - Your work around is just skipping over the problem.
slomojo
Delete this answer, it may confuse future visitors.
slomojo
I did change the setInterval call to 1.0 instead of 0.1 after I posted the script. The bug still persisted after that. So this most likely wasn't the problem.
Swissdude