ryan
ryan

Reputation: 45

setting variables to be used in another function

Using edge animate, I don't seem to have the control over orders of operations that I am looking for. I have a function that operates a switch and another that tests the conditions of one or many switches. The problem I am running into is edge keeps wanting to run the test before the switch.

I can have the switch launch the test but run into the issue that I load the objects in an array inside of edge. What I am thinking is I need a way of pre-loading variables that the function can use but they don't need to be global since they are only used in this one function.

Here is what I have so far.

Inside Edge Animate:

twoPhaseSwitch('.btn1','on'); //sets up switch 1
twoPhaseSwitch('.swtch1','off'); //sets up switch 2
conditionsArray(['.btn1','.swtch1']); // tells the test script what the buttons are

In JavaScript file:

function twoPhaseSwitch(object,state)
{
    var obj = $(object);
    stage.getSymbol(obj).stop(state);
    obj.data('state',state);

    obj.mousedown(function(e)
    {
        if(obj.state == 'off')
        {
            stage.getSymbol(obj).stop('on');
            obj.state = 'on';
            obj.data('state','on');
        }else{
            stage.getSymbol(obj).stop('off');
            obj.state = 'off';
            obj.data('state','off');
        };
    });
};

function conditionsArray(obj)
{
    var answers = ['on','on'];

    // compare lengths
    if (obj.length != answers.length)
    return 'Argument Miscount';

    var challengResults = challangeArray();

    if (challengResults == true)
    {
        lightOn('.LED1','on');
    }else if(challengResults == false)
    {
        lightOn('.LED1','off');
    };
    console.log(challengResults);

    function challangeArray()
    {
        for (var i = 0, l=obj.length; i < l; i++) 
        {
            if ($(obj[i]).data('state') != answers[i]) { 
                return false;
            }      
        }
        return true;
    };
};

function lightOn(lightOBJ,state)
{
    lightOBJ = $(lightOBJ);
    stage.getSymbol(lightOBJ).stop(state);  
};

I use mousedown and mouseup currently to fake the order of operations but it brings some pretty unacceptable issues so I am trying to do this right.

Upvotes: 0

Views: 61

Answers (1)

mseegan
mseegan

Reputation: 11

did you try wrapping your code in

$( document ).ready(){ your code here }

to prevent any javascript from running until the page loads?

Upvotes: 1

Related Questions