1

I have a function that measures time execution of function, and cleans DOM after each execution:

function measureTimeExecution(domID, testFunc){
    console.time("timer");
    for(var i = 0; i < 10; i++){
       testFunc();
       var getDiv = document.getElementById(domID);
    }
    getDiv.empty();
    console.timeEnd("timer");
}

Function that creates new ul

 function createList_Task_2(divID){
        var createNewUL = document.createElement("ul");
        createNewUL.id = "phoneList";
        document.getElementById(divID).appendChild(createNewUL);
        for(var i = 0; i < phones.length;i++){

            var chunk = "<li>" + phones[i].age +"<br>" + phones[i].id +"<br><img src='"
                + phones[i].imageUrl  +"'/><br>"  + phones[i].name + "<br>" + phones[i].snippet + "</li>";

            document.getElementById("phoneList").innerHTML += chunk;
        }
    }

But iy gives me: Uncaught TypeError: testFunc is not a function;

Example:

measureTimeExecution("div1", createList_Task_3("div1"));

Is it possible to get somehow domID in measureTimeExecution as a argument of testFunc?

2 Answers 2

3

the problem is that when you are calling measureTimeExecution you are runing the parameter, instead pass a function again.

look at this code it should work

measureTimeExecution("div1", function () { createList_Task_3("div1"); });
Sign up to request clarification or add additional context in comments.

Comments

3
function measureTimeExecution(domID, testFunc)

The function expects the second argument to be a function, but calling it like measureTimeExecution("div1", createList_Task_3("div1"));, it provides the return of createList_Task_3("div1"). Since createList_Task_3 returns nothing, the default return is undefined.

For it to be a function as well as be able to be provided the ID, it should return a function like this:

function createList_Task_2(divID){
  return function(){
    var createNewUL = document.createElement("ul");
    createNewUL.id = "phoneList";
    document.getElementById(divID).appendChild(createNewUL);
    for(var i = 0; i < phones.length;i++){

        var chunk = "<li>" + phones[i].age +"<br>" + phones[i].id +"<br><img src='"
            + phones[i].imageUrl  +"'/><br>"  + phones[i].name + "<br>" + phones[i].snippet + "</li>";

        document.getElementById("phoneList").innerHTML += chunk;
    }
  }
}

Comments

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.