Alnedru
Alnedru

Reputation: 2655

Javascript: Executing 2 functions after each other onmouseover event

I have a question regarding the jquery/javascript: I'm in the following situation:

Note I'm not allowed to modify that FirstFunction(), as it executes some different important tasks. What I want to do:

I wrote a following script:

function SecondFunction(obj) {
    a = $(obj).find("img");
    if (a != null && a.attr("src")!= null)
        if (a.attr("src").lastIndexOf("img_4") != -1)
            a.attr("src", "newSmallImg.png");
        else if(a.attr("src").lastIndexOf("img_2")!=-1){
            a.attr("src", "newBigImg.png");
        }
}

Normally when I debug the script, it works etc etc, but the image is not replaced back. And i've noticed that "SecondFunction" is finished before the "FirstFunctions" is finished executing.

Can I do something about it? Like setting in SecondFunction => FirstFunction.ready? (The first function is also in a different file.)

Any input would be welcome

Small update, I'm calling the functions as follows:

<a onmouseover="FirstFunction(); SecondFunction(this);" ...>
    <span>
       <img src="" ... />
    </span>
</a>

Upvotes: 0

Views: 423

Answers (2)

Ivan Turovets
Ivan Turovets

Reputation: 164

Can I do something about it? Like setting in SecondFunction => FirstFunction.ready? (The first function is also in a different file.)

Possible this helps you

<a onmouseover="FirstFunction(this, SecondFunction)" ...>
    <span>
       <img src="" ... />
    </span>
</a>

And change FirstFunction() like this

function FirstFunction(obj, callback) {
    //your code here

    if(callback && typeof callback === 'function') {
        callback(obj);
    }
}

Upvotes: 0

kothvandir
kothvandir

Reputation: 2161

1) check the object type you're receiving in your second function, how are you calling this function? can you provide the code?

2) check that the image your are finding in this line, is the one you expect:

a = $(obj).find("img");

I recommed to access the objects by an unique id:

var myImge = $('#imageId')

Upvotes: 1

Related Questions