Search code examples
ajaxpolymertimeout

How to Handle 'iron-ajax` Timeout


The iron-ajax web component has a timeout property, but after reading through the components code on GitHub I am not sure how to handle it.

<iron-ajax id="ajax"
  handle-as="json"
  last-response="{{response}}"
  method="get"
  timeout="5000"
  url="https://api.domain.com/">
</iron-ajax>

Does it fire an event?

Is it observable?

How can I run a function when a request reaches its timeout limit?


Solution

  • The timeout property on iron-ajax is the number of milliseconds a request can take before automatically being terminated. As this timeout is an error the iron-ajax element will fire an error event which you can use to call a function when triggered. For instance:

    <iron-ajax id="ajax"
      handle-as="json"
      last-response="{{response}}"
      method="get"
      timeout="5000"
      url="https://api.domain.com/"
      on-error="_showError">
    </iron-ajax>
    

    ...

    class MyElement extends Polymer.Element {
      static get is() { return 'my-ele'; }
    
      ...
    
      _showError(event, request) {
        // display error message
      }
    }