I have the following code used to show a spinning gif while an image is being uploaded when a new post is submitted:
$('#new_post').on 'submit', ->
$(document).ajaxStart ->
$("#loading-indicator").show()
$(document).ajaxStop ->
$("#loading-indicator").hide()
However, there is an ajax request that fires every second for message updates, so when you first fire the image upload button, the spinner is going to be constantly showing because of the global effect of ajaxStart, this is the message update check code:
$(document).ready ->
$(document).on 'click', '#list-mensagens.list-mensagens li', (e) ->
location.href = $(this).data('href')
check_conversa = ->
$.get "/conversas/novas", (data) ->
qtd = data.quantidade
$('.menu-usuario .notificacoes').html qtd unless qtd == 0
$('.menu-usuario .notificacoes').html '' if qtd == 0
if true
setInterval check_conversa, 1000
check_conversa()
What is the best way to make the spinner show and not have the ajaxStart method fire during every check request?