JavaScript How do I make a div change its width periodically using JS? make div change width,make padding not change width

I have a temperature scale (green up to some extent, then yellow and then red) in the background and need a temperature "bar" of sorts over it which:

  1. Refreshes after a fixed interval of time, in the way that it "updates" itself periodically
  2. Changes its width dynamically according to the parameters that it reads. For now I'm just trying to do it with a random number generator (within a range)

I did something like this, created a div here

<div id="temperatureBar" style="background-color: black; height: 20px;"></div>

and used some jQuery

function refreshTemperature(){
    var width = (Math.random() * (100 - 1)) + 1;
    setInterval(function () {
        $('#temperatureBar').fadeOut('slow');
        $(document).getElementById('temperatureBar').style.width = width+"px";
        $('#temperatureBar').fadeIn('slow');
        refreshTemperature();
    }, 5000);
}

This ain't working though. Can anyone help me with a workable solution?

Answer:1

You should use the $.css method of jquery.

Another thing which you may not have noiticed is that setInterval is called every 5 seconds in your code, and each call starts another setInterval.
This means that after a couple of minutes, you will have a huge number of events waiting to be called and this will lead to all sorts of problems. Use setTimeout instead.

Here's a working sample:

function refreshTemperature() {
  setTimeout(function() {
    var elem = $('#temperatureBar')
    var width = (Math.random() * (100 - 1)) + 1;
    elem.fadeOut('slow');
    elem.css('width', width + "px");
    elem.fadeIn('slow');
    refreshTemperature();
  }, 5000);
}

refreshTemperature()
#temperatureBar {
  background-color: tomato;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="temperatureBar">
  My size changes
</div>
Answer:2

I updated your js code. Now the width changes periodically.

function refreshTemperature(){
    var width = (Math.random() * (100 - 1)) + 1;
     $('#temperatureBar').fadeOut('slow');
        $('#temperatureBar').css('width', width + "px");
        $('#temperatureBar').fadeIn('slow');            
}

setInterval(function () {        
    refreshTemperature();
}, 5000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="temperatureBar" style="background-color: black; height: 20px;"></div>
Answer:3

When writing a gallery faced with a problem. There is pagination for the gallery. Each page should be different picture. As with the aid of AJAX upload new content to the gallery when switching pages? ...

When writing a gallery faced with a problem. There is pagination for the gallery. Each page should be different picture. As with the aid of AJAX upload new content to the gallery when switching pages? ...

  1. load content with ajax
  2. load content with javascript
  3. load content with jquery
  4. load content with ajax wordpress
  5. load content without page refresh
  6. load content with ajax jquery
  7. load content with scroll
  8. load content from url jquery
  9. load content in iframe
  10. load content in div jquery
  11. load content from another page javascript
  12. load content in div
  13. load content from url javascript
  14. load content in iframe jquery
  15. load content in div ajax
  16. load content from url jquery ajax
  17. load content from file php
  18. load content from url php
  19. load content from file javascript
  20. load content from file c#

I want an object like var obj = { "ABC" : { name: true, dob: true}, "CDE" : { name: true, dob: true}, "EFG" : { name: true, dob: true}, "CBA" : { name: true, dob: true}, "XYZ" : { ...

I want an object like var obj = { "ABC" : { name: true, dob: true}, "CDE" : { name: true, dob: true}, "EFG" : { name: true, dob: true}, "CBA" : { name: true, dob: true}, "XYZ" : { ...

  1. create object from array javascript
  2. create object from class
  3. create object from interface typescript
  4. create object from json
  5. create object from string
  6. create object from type c#
  7. create object from class python
  8. create object from dict python
  9. create object from spreadsheet salesforce
  10. create object from object javascript
  11. create object from string python
  12. create object from map javascript
  13. create object from class javascript
  14. create object from array lodash
  15. create object from json string
  16. create object from xml c#
  17. create object from another object javascript
  18. create object from interface java
  19. create object from file java
  20. create object from json java

I need to set for a div a margin-left in JavaScript in such a way that its value if the minimum between two lengths in different units, e.g. min(10pt,15vw). There is Math.min() that we can use to ...

I need to set for a div a margin-left in JavaScript in such a way that its value if the minimum between two lengths in different units, e.g. min(10pt,15vw). There is Math.min() that we can use to ...

I am trying to understand what causes a simple jquery function to fail. It simply looks for the ID, replaces some content with other data. I have $data1, $data2, $data3 as the replacement test data ...

I am trying to understand what causes a simple jquery function to fail. It simply looks for the ID, replaces some content with other data. I have $data1, $data2, $data3 as the replacement test data ...