Ad

Function Is Undefined, But I Had Defined It

- 1 answer

I'm a PLC programmer, i made a html page and i need it script refresh every second, so in my body i placed:

    <body onload="changeimage()">

.... code ....

</body>

And the script:

<script>
setInterval(function changeimage() {
var sonde = ["Sonda1","Sonda2","Sonda3","Sonda4"];
var tsonde = ["tsonda1","tsonda2","tsonda3","tsonda4"];
var temperature = [:="OUTPUT".AlarmTemp.Sonda1:,:="OUTPUT".AlarmTemp.Sonda2:,:="OUTPUT".AlarmTemp.Sonda3:,:="OUTPUT".AlarmTemp.Sonda4:];
var hotcold = document.getElementById("Fiocco");
if (:="OUTPUT".Stagione.Estate: > 0){
      hotcold.src="sun.jpg"}
if (:="OUTPUT".Stagione.Inverno: > 0){
      hotcold.src="Neve.png"}    
 for (x in temperature) {
           var icona = document.getElementById(sonde[x]);
                     if (temperature[x] > 0 ){
               icona.src="Paverde.png"
           }
           else{
               icona.src="Parossa.png"
           }

     }
}, 1000);   
</script>

Anyway i get the error: ReferenceError: changeimage is not defined

P.S.: Don't get fooled by the arrays who start with ":", is a PLC syntax, is correct.

@ Even removing the setinterval option, the script is not working.

Ad

Answer

Define the function first and make sure the script is loaded.

Example in code pen: https://codepen.io/mikila85/pen/NWPvQPE

function changeimage() {
    setInterval(function() {
        var sonde = ["Sonda1","Sonda2","Sonda3","Sonda4"];
        var tsonde = ["tsonda1","tsonda2","tsonda3","tsonda4"];
        var temperature = [:="OUTPUT".AlarmTemp.Sonda1:,:="OUTPUT".AlarmTemp.Sonda2:,:="OUTPUT".AlarmTemp.Sonda3:,:="OUTPUT".AlarmTemp.Sonda4:];
        var hotcold = document.getElementById("Fiocco");

        if (:="OUTPUT".Stagione.Estate: > 0){
            hotcold.src="sun.jpg"
        }
        if (:="OUTPUT".Stagione.Inverno: > 0){
            hotcold.src="Neve.png"
        }

        for (x in temperature) {
            var icona = document.getElementById(sonde[x]);
            if (temperature[x] > 0 ){
                icona.src="Paverde.png"
            }
            else{
                icona.src="Parossa.png"
            }

        }
    }, 1000);
}
Ad
source: stackoverflow.com
Ad