0

i have a page which is displaying a temperature number from a mysql DB and a slider which gets its value from the same database. I would like to run both the DB query and display the values on page start so I get a temperature and slider value straight away. The the temperature will refresh every 5 seconds but the slider value will do this every couple min. I could probably get this working ok with setInterval but have been warned about this causing delay stack ups so i am trying setTime out but I cannot get it working (first time using this)

<script>
var gaugeData = function(){
     $.ajax({
        url:"data.php",
        dataType:"json",
        success: function(data){
            console.log(data);
            $('#thermostatSlider').val(data.thermostat);
            if (data.temp <= 15){
                document.getElementById("RoomTemp").style.color = '#00FFFF';
            }
            if (data.temp > 15 && data.temp < 21){
                document.getElementById("RoomTemp").style.color = '#FF9900';
            }
            if (data.temp >= 21){
                document.getElementById("RoomTemp").style.color = '#FF3300';
            }
            $("#RoomTemp").html(data.temp + '&deg;C');
            sliderFunction();
        }
    });
 }
function sliderFunction(){
    var currentValue = $('#currentValue');
    $('#thermostatSlider').change(function(){
        currentValue.html(this.value);
    });
    $('#thermostatSlider').change();    
};
setTimeout(sliderFunction, 9000);
setInterval (gaugeData, 5000);

Amy help please

  • If they will both need to refresh an 'infinite' number of times, then you're looking for `setInterval`. If you want it to appear immediately at page start, just call `sliderFunction()` / `gaugeData()` directly -- it will only run once, at page load. Then the `setInterval` function will trigger to update it every `x` many milliseconds. You can call it directly **and** make use of `setInterval`. – Obsidian Age Jan 14 '18 at 20:51
  • I need it to load on page start the a net interval different for each function. setInterval will work but I have read this can cause a stuck up of delays which might cause the page to stop working after a while –  Jan 14 '18 at 20:57
  • The only reason that `setInterval` is 'bad' is because it executes regardless of the thread (meaning it can 'miss' the timing). Depending on how long the animation is, you could always chain `setTimeout` to a function that calls it every one millisecond, as is demonstrated in [**this answer**](https://stackoverflow.com/a/5479821/2341603). – Obsidian Age Jan 14 '18 at 21:15

0 Answers0