Flash“这部电影中的脚本” - 由loadVars或setInterval引起的?

发布于 2024-09-28 13:21:15 字数 1820 浏览 6 评论 0原文

这有点奇怪...

我有一个加载外部 SWF 的 Flash 文件。在那里,我使用 LoadVars 从服务器获取外部数据。当数据到达时,我使用 setInterval 来调用淡入内容的函数。

到目前为止,一切都很好。

但是,当我在所有数据到达之前切换菜单(也称为卸载一部电影以加载另一部电影)时,Flash 会挂起大约 15 秒,然后向我显示臭名昭著的消息“这部电影中的脚本正在减慢 Flash - 你想中止吗?”它?” (粗略翻译)。

我在 SWF 中附加了一个“onUnload”函数,这导致了主要问题,其中我使用clearInterval 来消除间隔并重新实例化 LoadVars 变量。

没有任何帮助...

有什么想法吗?

根据请求 - 这是导致问题的 SWF 的来源:

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();

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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇女薄笑 2024-10-05 13:21:16

更新

首先,我建议您放弃使用 setInterval,它们特别有问题。而是使用 Timer 类,它的管理和使用要简单得多。

不过,sRes.onLoad 函数内的setInterval 可能是您的问题。间隔应以毫秒为单位,因此您的 0.1 可能应为 100(1/10 秒),并且 fadeInunLoad 中的 clearInterval 调用> 可能有 myInterval 超出范围,请使用 trace 检查它们以确保它们按预期清除。

代码

好吧,我已经重新排序了代码以使其更具可读性......但是,它的形状仍然很糟糕。我正在标记需要与您进一步讨论的内容...(请参阅下面代码中的其他注释。)

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...");
    }
}

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...");
    }
}
┊风居住的梦幻卍 2024-10-05 13:21:16

感谢您的帮助 - 似乎我发现了这个怪癖...

即使电影被卸载,sendAndLoad-response 仍然被调用,似乎导致了循环。

我所做的是:

在脚本顶部添加一个名为“doParse”的变量并将其设置为 true

var doParse = true;

然后在卸载函数中,我将此 var 设置为 false:

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

如果数据到达时被调用,我添加了这个:

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

这有帮助,消息消失了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文