Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
Custom jQuery function randomly undefined

Thank you in advance for your help.

I have a page that renders a handful of charts but I seem to be experiencing a scope issue only once in a while.

I have a series of jQuery functions I made to render specific types of charts based on options passed in, each one contains an AJAX call to request the proper data-set based on the options submitted when calling the chart function on the chart container.

Some of the charts are paired with a second chart whose options are determined by the JSON response of the first charts AJAX call response. The paired chart is rendered in a callback in the first chart, after the AJAX success: is triggered.

Most of the time this works perfectly fine but occasionally I get the error "TypeError: $(...).lineChart is not a function". The first barChart renders every time, but once in a while the lineChart function that is defined in the same file as barChart is no longer accessible.

Any delay I add into the code seems to fix the issue. The issue does not exist when I use the debugger and I can catch the error and re-call the lineChart function after a timeout which seems to work as well.

I'm at a loss as to why the function would no longer be available sometimes and what seems randomly.

I apologize for not pasting in the exact code. There are a fair bit of comments and variable/function names I cannot post here.

Thanks.

$.fn.barChart = function (options, callback) {
    $.ajax(options["url"], {
        method: "POST",
        type: "json",
        data: options.data,
        success: function (response) {

            // ..render this chart ..

            var optionsForNewChart = {}; // ..generate new options to respond with base on response JSON
            callback(optionsForNewChart);
        }
    });
};

$.fn.lineChart = function (options) {
    $.ajax(options["url"], {
        method: "POST",
        type: "json",
        data: options.data,
        success: function (response) {
            // ..render this chart ..
        }
    });
}


$("#barChart1").barChart({
    url: "https://...",
    data: {
        //post data
    }
}, function (response) {
    $('#lineChart1').lineChart(response);
});

$("#barChart2").barChart({
    url: "https://...",
    data: {
        //post data
    }
}, function (response) {
    $('#lineChart2').lineChart(response);
});

$("#barChart3").barChart({
    url: "https://...",
    data: {
        //post data
    }
}, function (response) {
    $('#lineChart3').lineChart(response);
});
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Enclose your code in : $(document).ready(function(){ //YOUR CODE GOES HERE }); This will solve the issue of the function being not loaded.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!