English 中文(简体)
• 如何发挥嵌入功能,以多次运行
原标题:How to get an embedded function to run multiple times
  • 时间:2012-04-07 17:20:52
  •  标签:
  • javascript

我提出的问题是,我如何掌握多种职能。 下面是我的职能——一个简单的缺陷功能。 问题 我不得不说,当它第二次放弃了第一次呼吁。 因此,如果用户在一顿点点上点击,就会展示出一个 message的信息。

如果用户点击另一个纽州,则以前的散射电文在目前的不透明度上只是停留。

<>?

What is the stopping mechanism? Where did the previous function go?

www.un.org/spanish/ecosoc 职能

function newEffects(element, direction, max_time ) 
{
    newEffects.arrayHold = [];
    newEffects.arrayHold[element.id] = 0;

    function next() 
    {
        newEffects.arrayHold[element.id] += 10;
        if ( direction ===  up  )
        {
            element.style.opacity = newEffects.arrayHold[element.id] / max_time;
        }
        else if ( direction ===  down  )
        {
            element.style.opacity = ( max_time - newEffects.arrayHold[element.id] ) / max_time;
        }
        if ( newEffects.arrayHold[element.id] <= max_time ) 
        {
            setTimeout( next, 10 );
        }
    }
    next();
    return true;
};

The Call

newEffects(this.element,  down , 4000 ); 

Old Object

/**
 *    Effects - causes all elements that are fading to sync. to same opacity level as they share the same elapsed time.
 */

var Effects = function( element ) 
{
    this.element = element;
};

Effects.prototype.fade = function( direction, max_time ) 
{
    Effects.elapsed = 0;
    var persist_element = this.element;
    function next() 
    {
        Effects.elapsed += 10;
        if ( direction ===  up  )
        {
            persist_element.style.opacity = Effects.elapsed / max_time;
        }
        else if ( direction ===  down  )
        {
            persist_element.style.opacity = ( max_time - Effects.elapsed ) / max_time;
        }
        if ( Effects.elapsed <= max_time ) 
    {
            setTimeout( next, 10 );
        }
    }
    next();
    return true;
};
最佳回答

引言

function newEffects(element, direction, max_time ) 
{
    var mt=max_time;
    var dir=direction; 
    var el=document.getElementById(element);
    var newEffects={};
    newEffects.arrayHold = [];
    newEffects.arrayHold[el.id] = 0;

    function next() 
    {
        newEffects.arrayHold[el.id] += 10;
        if ( dir ===  up  )
        {
            el.style.opacity = newEffects.arrayHold[el.id] / mt;
        }
        else if ( dir ===  down  )
        {
            el.style.opacity = ( mt - newEffects.arrayHold[el.id] ) / mt;
        }
        if ( newEffects.arrayHold[el.id] <= mt ) 
        {
            setTimeout( next, 10 );
        }
    } next();
};

例如:here

Declare every variable using var keyword inside the parent function to keep separate instance of each items, this is how clouser works.

问题回答

它希望你再次下届。 我感到惊讶的是,你的代码是坠毁的。

考虑这一法典:

function foo() {
    foo.bar = 5;
    alert(foo.bar); // alerts 5
}
foo();
alert(foo.bar); // alerts 5 instead of undefined!

oo是一种具有特性的物体,但也可以称为功能。 即便在职能要求之外,所设定的任何财产都将继续存在。 (事实上,这是制作的一种方法。) Javascript pseudo-qu,对自身用途有用。

相反,你可以做的是,以以下两种方式之一恢复这一“阶级”的新事例:

function fadeEffect(element) {
    this.element = element;
    this.fadeTimeLeft = 1000;
    this.fade = function() {
        this.fadeElement.style.opacity -= .01; // Replace with working
                                               // opacity decreaser.
        this.fadeTimeLeft -= 1;
        if (this.fadeTimeLeft > 0) {
            setTimeout(this.fade, 10);
        }
    }
}

signInButtonFade = new fadeEffect(document.getElementById("signinfade"));

Or in Javascript s Object-literal notation, which You may have better as JSON :

function fadeEffect(element) {
    return {
        "fadeElement": element,
        "fadeTimeLeft": 1000,
        "fade": function() {
            this.fadeElement.style.opacity -= .01; // Replace with working
                                                   // opacity decreaser.
            this.fadeTimeLeft -= 1;
            if (this.fadeTimeLeft > 0) {
                setTimeout(this.fade, 10);
            }
        },
        "init": function() {
            // When using this format, no code is executed automatically,
            // so we have a "constructor" that we execute manually when
            // making the object.
            this.fade();
        }
    }
}

signInButtonFade = generateFade();
signInButtonFade.init();

(实际上,实际的不透明性下降并不可行)。)

你们还可以将每一种缺陷的影响储存在一个阵列中,然后检查一下,是否对某个要素适用了以前的任何不利影响。 如果存在的话,那么你要么可以阻止这种疏漏或劫持,要么完全放弃。

fadeArray = new Array();
fadeArray.push(new fadeEffect(document.getElementById("fadeElement"));

// Do other adding fade stuff, etc...

// Now we add a new fade effect. This could easily be put into a function:
fadeEffectAlreadyExists = false;
for (i in fadeArray) {
    if (i.element == document.getElementById("fadeElement2")) {
        fadeEffectAlreadyExists = true;
        break;
    }
}
if (!fadeEffectAlreadyExists) {
    fadeArray.push(new fadeEffect(document.getElementById("fadeElement2")));
}

While this is fine and all (and in my opinion, the correct way to do it), you can probably keep your existing function by replacing each instance of "newEffects" in the function body with "this", and then use fade = new newEffects(element, direction, max_time); since it already shares quite a bit in common with the first approach.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签