Chartjs-How to lazy evaluate and execute javascript?

0👍

Anything in a function definition is not executed until the function is called. hence you can wrap your tab specific code in functions and call them whenever you open that tab.

in order to run a chart in chartjs you need to write the following:

var myChart = new Chart(SELECTOR, {...})

hence only call new Chart on your active selectors

//Global Configs
Chart.defaults.global.hover.mode = 'single';

function onTabOne() {
    var myChart = new Chart(SELECTOR, {...})
}
function onTabTwo() {
    var pieChart = new Chart(SELECTOR, {...})
}

NOTE
evaluation of javascript is super fast and is not heavy on machine, its basically hoisting and memory allocation for variables, you should not worry about lazy evaluation, and there is no direct way. but execution is where the heavy work takes place, and you can delay execution by wrapping things in function definitions.

there are hacky ways to get lazy evaluation, such as putting javascript in strings and using eval whenever you want to evaluate and execute the code, but i dont recommend using eval at all.
and there is no real need to do it.

Leave a comment