0

I have 2 js files. In the first one I have this:

var functionName = "video"; 
var cont = 1;

$(function() { 
    window.control = function control() {   
        var tipo1 = functionName + cont + "();";
        var tipo2 = eval(tipo1);
        tipo2;
        cont++;
    });

In the second one:

function video1() {
    control();
}

function video2() {
    control();
}

The first time was fine, but in the second, first execute video1() and then video2(), why?

2
  • This isn't going to fix the issue, but please don't use eval(). Try window[tipo1](); instead. Note you may need to change window depending on your logic structure. Commented Jul 2, 2015 at 10:56
  • thanks for the advice, but if I call window[tipo1](); there is an error in console: "window[tipo1] is not a function" Commented Jul 2, 2015 at 11:15

1 Answer 1

3

Your definition is wrong:

window.control = function control() { 

I imagine because of this it's firing control() execution.

Change this to:

window.control = function() { 

Also I see no reason for defining this function at DOM ready state. It will just cause confusion and potential reference issues. The definition of a function is only ran at execution point, these should potentially be on DOM ready state depending on their use.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, but don't use to me. I remove window and I just have function control(). But it is the same, first execute video1 and then video2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.