I am using Rails 5.2.2 and I would like to set a timeout when using Rails UJS so to display an alert message on request timeout.
I read the Official Guide for working with JavaScript and an answer related to this issue but the guide writes nothing about the timeout and the answer is dated.
In my .html.erb view file I'm using:
<input id="element_dom_id" type="checkbox" data-remote="true" data-url="/a_url" data-params="key=value" data-method="patch">
<script type="text/javascript">
document.getElementById('element_dom_id').addEventListener(
'ajax:success', function(event) {
...
}
);
document.getElementById('element_dom_id').addEventListener(
'ajax:error', function(event) {
...
}
);
...
</script>
Try this:
You can use the $.rails.ajax property from jquery-ujs in order to inject the timeout if it hasn't been passed explicitly.