0

How to time user-created JavaScript?

I created a script which enables me to set a Klipfolio variable by monitoring the URL query string. My script works, but I'm struggling with getting it to time correctly. If I set the script to run when the DOM has loaded, Klipfolio is still running its own scripts which seem to override my changes. If I delay the script by 5 seconds, it correctly changes the variable I want to change, but of course using a static time as a delay is not a viable option. 

Here's my code (it's embed inside a "HTML Placeholder" Klip):

 

var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;

for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');

if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
};

$( document ).ready( function() {
var PROD = getUrlParameter('prod');
var BETA = getUrlParameter('beta');

var changeKlipfolioVariable = function changeKlipfolioVariable () {
if (PROD == true) {
$("select").val('DataWarehousePROD').change();
console.log( "Switched variable to PROD" );
} else if (BETA == true) {
$("select").val('DataWarehouseBETA').change();
console.log( "Switched variable to BETA" );
}
}
changeKlipfolioVariable();
//setTimeout(changeKlipfolioVariable, 5000)
console.log( "Variables - PROD: " + PROD + " BETA: " + BETA );
});

 

PasteBin URL for better readability: https://pastebin.com/f79pvmxS

 

My questions:

Is there a help page / tutorial for using JavaScript with Klipfolio?

Is there an event or something like that inside Klipfolio which I could use to time my script correctly?

Are user created scripts ran only once upon opening the dashboard or is there a possibility they run again after data gets refreshed etc.?

 

Regards,

Carlos Kynäslahti

 

 

1 comment

  • 0
    Avatar
    Josh Cohen-Collier

    Hi Carlos,

    Thanks for posting in our Community!

    Unfortunately, as far as I am aware, there is no way to trigger the variables being set after the Klipfolio script is run.
    However, this script is run at the very beginning of the page being loaded, meaning that a wait period of a few seconds should actually be good in all cases.

    Hope this helps!

    Best regards,
    Joshua
    _________________
    Joshua Cohen-Collier,
    Technical Support Specialist, Klipfolio

Please sign in to leave a comment.