At the moment, I am working on a block of code that triggers every time a specific ajax call is made.
The snippet I'm using looks like this (although it might throw an error due to absence of html or css - but that's not my concern as I just want to showcase my code):
$(document).ajaxStart(function() {
if ($('.spinner').length < 1) { //checking for existing spinner
$('body').prepend('<div class="spinner" title="Spinner stuck? Click to remove!"></div>');
}
$('body div').not('.spinner').css('opacity', '0.5');
});
.done(function(xhr) {
$('body div').not('.spinner').css('opacity', '0');
$('body').one('transitionend', function(e) {
$('body div').not('.spinner').remove();
$('body').append(xhr);
$('body div').not('.spinner').css('opacity', '0.1');
$('body').one('transitionend', function(e) {
$('body div').css('opacity', '1';
$('.spinner').remove();
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
This setup features nested .one('transitionend')
s which I'm analysing to determine its correctness. This piece of code handles login and logout responses from ajax calls. While it works well for logging in most cases, it seems to be failing for logout due to issues with registering the end transition. Consequently, the process freezes at
$('body div').not('.spinner').css('opacity', '0.1');
.
I've created a quick test on this jsfiddle to compare nested versus unnested event handlers.
The two approaches seem to yield similar results. However, early on, there were freezing issues when clicking too frequently on either - this was addressed by adding the log bit -
$('#log span').text($('#div1').text());
, etc.
Here's a snippet of the updated jsfiddle:
//code snippet here...
//more code snippets here...
In light of these experiments, I aim to identify the superior method. Which option offers smoother execution and fewer problems for the code or browser? Is there a more effective way to manage code reliant on transitions or animations?
My attempts at utilizing jQuery's .queue and .delay have not yielded results in this context. Perhaps another approach could prove fruitful? Although setTimeout()
remains a possibility, maintaining synchronicity may require nesting elements below it. Moreover, while .one('transitionend')
appears functional in most instances irrespective of nesting, some scenarios demand further investigation.
UPDATE:
In response to feedback provided on my code:
Some minor pointers on your code, don't do css in Jquery but append/prepend classes with the opacity value. Use variables instead of going through the DOM with every selector. – Jan_dh
I have revised the corresponding jsfiddle to adjust classes rather than directly modifying CSS properties. You can access the updated version through this link: here. However, I seek clarification on:
Use variables instead of going through the DOM with every selector.
If this notion aligns with addressing my query, I welcome insights from individuals who comprehend Jan_dh's suggestion.