Checkout Page Php Function

Hi,
I would like your help with a problem that i have when i'm trying to execute a php function in the checkout page.
I have the code below which is added on the scripts.post.tpl file:

The code and the function is running fine without any errors. The problem is that every time a customer change the payment or the shipping method, the function runs again and again after the ajax load.
Is there any other .tpl file that i can add my script in order to be executed only once, or maybe any way to stop if from running again after ajax calls?

E.g.

{if !$smarty.const.AJAX_REQUEST}

{/if}

Thank you a lot eComLabs, that worked perfectly!