0

I have the following function:

$(function(){
    performance();
    function performance(){
       pHandler = setTimeout(performance,3000)
        perOb = [];
        alert('hhhh')
        $(".performance").each(function(i){            
            perOb[i] = $(this);
            url = '/cavity/performance/'+perOb[i].attr('data-id')+'/'+jobId;
            //perOb[i].html('%');
            $.ajax({
                type: "GET",
                //dataType: "json",
                url: url,
                success: function(data){
                    perOb[i].html(data.performance+"%");
                },
                error: function(xhr, status, response){
                    console.log(xhr.responseText);

                },

            });
        });
    }

});

I'm trying to call it from another event like the following:

$('#in-between').change(function(){
        if ($(this).prop('checked')){
            window.clearTimeout(pHandler);
            alert('yesr')            
        }
        else{
            alert('noo')
            performance();
        }

    })

I have got the error performance is not a function. I have tried $.performance(), jQuery.performance() and also I have tried to assign it to a variable like:

perf = $(function(){
    performance();
    function performance(){
       pHandler = setTimeout(performance,3000)
     .......

And calling it as perf.performance() but all of those trying don't succeeded to call it from the event.

This question is differ than JavaScript error: "is not a function" for the following:

It is meant by Jquery so someone may, mistakenly, regard that multiple document.ready(function()) as one scope for Jquery

SaidbakR
  • 13,303
  • 20
  • 101
  • 195
  • 3
    Define `performance` in a scope that is shared by both event handlers (likely the global scope in your case). In other words: move `function performance() {}` outside of `$(function() {})`. Have a look at: [What is the scope of variables in JavaScript?](https://stackoverflow.com/q/500431/218196) – Felix Kling May 22 '17 at 17:17
  • 2
    You need to read a good primer on scope in JavaScript. `performance` is only defined inside the function in which you've defined it. – Heretic Monkey May 22 '17 at 17:17
  • Possible duplicate of [JavaScript error: "is not a function"](https://stackoverflow.com/questions/9825071/javascript-error-is-not-a-function) – Heretic Monkey May 22 '17 at 17:18
  • @MikeMcCaughan Please checkout the quote updated to the question. – SaidbakR May 22 '17 at 17:39
  • The fact that someone may be confused about the fact that two functions are not the same as one doesn't make the question any less of a duplicate. – Heretic Monkey May 22 '17 at 17:42
  • @MikeMcCaughan I agree with you that the two questions share the same concept but different symptoms. i.e the same concept may be found in PHP and Python, for example, but we could not describe them duplicates. The trick in syntax different between the Javascript and its library Jquery. – SaidbakR May 22 '17 at 17:46
  • There is no difference in syntax between JavaScript and jQuery. jQuery is written in JavaScript and therefore has the same syntax. – Heretic Monkey May 22 '17 at 17:48

1 Answers1

5

You've defined the function only within the scope of its parent function, which is an anonymous function that gets executed when the page loads:

$(function () { // the anonymous function
    function performance() {
        // your function
    }
});

If you want it to exist outside that scope, you have to define it outside that scope. For example:

// define the function
function performance() {
    // your function
}

// execute it when the document loads
$(performance);

That would define your function in a higher scope. This is likely in the global scope. If that's not desired, then you would wrap the entire context in which performance is needed in a larger function and self-invoke that one.

David
  • 208,112
  • 36
  • 198
  • 279
  • I followed your solution and it succeeded when I defined the `$('#in-between')` event in the same `document.ready()` that contains `function performance(){...` definition. Thank you very much. – SaidbakR May 22 '17 at 17:29