Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

72
Vistas
Display Image whenever an update is detected using JavaScript

I'm currently working on a website that has an image file that is frequently overwritten, but since the website doesn't automatically update the image when it is changed, I'm trying to re-display the image whenever it senses a change in the file.

Currently, this is the part that displays the image:

<script>
    const fs = require('fs');
   function updateImage() {
            var image = document.getElementById("img");
            image.src = image.src.split("?")[0] + "?" + new Date().getTime();
    }
    fs.watch(document.getElementById("img"), function(eventType, filename) {
        if (event == 'change'){
            updateImage();
        }
    });
</script>

I know from testing that updateImage() works on it's own, but I was wondering why the fs.watch() isn't detecting when the image file gets overwritten. Also, does fs.watch() constantly check for updates or do I need to constantly loop to check for updates?

Thank you!

7 months ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos