.error() in jQuery is not a funciton

Asked

Viewed 57 times

0

I inserted after loading all the content of my website the function below, and is returning an error message on the console.

Typeerror: $(...). error is not a Function

I tried to fix it using the jQuery.noConflict() but the error persists.

I’m using jQuery version 3.3.1.

$(document).ready(function(){
    $(function() {
       var $images = $('img.imageClassUpdateAtInterval:not([src="/assets/images/ico-loading.gif"])');

      // Now, no such image with
       // a spinner
       if($images.length === 0 && window.imageLocator)
         clearInterval(window.imageLocator);


        window.imageLocator = setInterval(function() {
            $images.each(function() {
                $this = $(this);
                if (!$this.data('src')) {
                    $this.data('src', $this.prop('src'));
                }

                $this.prop('src', $this.data('src') + '?timestamp=' + new Date().getTime());
                console.log($this.prop('src'));
            });
        }, 60 * 8000);

       // suppose, an error occured during
       // locating the src (source) of the
       // image - image not found, network
       // unable to locate the resource etc.
       // it will fall in each time on error
       // occurred 
       $('img.imageClassUpdateAtInterval').error(function () {   
                 // set a broken image
                 $(this).unbind("error").attr("src", "/assets/images/ico-loading.gif"); 
                 // setting this up in relative
                 // position
                 $(this).css("position", "relative");
                 $(this).apppend("<span><!--error--></span>");
                 $(this).find("span").css({"position": "absolute", "background-color": "#252525", "padding": ".3em", "bottom": "0"});
       });

    });
})
  • Have you checked whether the reference for jquery is loaded before running this script?

  • @Sérgiolopes is yes, the function runs after the file load.

1 answer

3


According to the official documentation, the event .error() was considered obsolete as of version 1.8 and removed from version 3.0 of jQuery.

Instead of that, use:

$('img.imageClassUpdateAtInterval').on("error", function(){
   // códigos
});

to trigger the event error.

No need to use .noConflict().

Tip: since you are using a very recent version of jQuery, stay tuned that many functions and methods of the older jQuery were removed from version 3.0, such as this .unbind() you’re using, which has been replaced by .off() (see here). When you come across an error, check the documentation to see what has changed.

  • 1

    Thank you for your patience @Sam

Browser other questions tagged

You are not signed in. Login or sign up in order to post.