Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

115
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda