I am using Inline Button Loader For Bootstrap 4 - Bootstrap4C Spinners to show loading action when clicked. I am also calling the click action manually in my javascript for every word that gets entered. Problem is when I type the word one by one and waiting for the response (which takes a second), the button transition works correctly. However if i type multiple words back to back without waiting for the response then the button always gets stuck in loading state.
You can observe the behavior here: http://34.66.22.154:8000/
Here is my index.html:
<html lang='en'>
<head>
<meta charset='utf-8'>
<link rel='stylesheet' href='../static/style.css'>
<link rel="stylesheet" href="../static/cmGauge.css">
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="../static/cmGauge.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css" rel="stylesheet">
<link href="../static/component-spinners.css" rel="stylesheet">
</head>
<body>
<div>
<div class='center'>
<div class='title'>Sentiment Analysis of Movie Reviews</div>
<div class='content'>
<form action="/analyze" id="analyzeForm" class="form" method="post">
<div class="form-group">
<textarea id = "review_id" rows = "10" cols = "100" name = "review_name" class="review_class"></textarea><br>
</div>
<div class='analyze'>
<button id="submit_button" type="submit" class="btn btn-primary btn-spinner btn-spinner-example" value="Analyze" data-spinner-text="Analyzing...">Analyze</button>
</div>
</form>
<b>Negative</b>
<div id="gaugeDemo" class="gauge gauge-big gauge-green">
<div class="gauge-arrow" data-percentage="40"
style="transform: rotate(0deg);"></div>
</div>
<b>Positive</b>
<div class='result'>
<br><p id="result"></p>
</div>
<div id="result2"></div>
<script type="text/javascript">
var globalWordCount = 0
var indicator = 50
var btn
var gauge = $('#gaugeDemo .gauge-arrow')
gauge.cmGauge();
gauge.trigger('updateGauge', indicator);
$('.btn-spinner-example').click(function() {
console.log("Button click start");
btn = $(this);
$(btn).buttonLoader('start');
});
function word_count(field, count) {
var number = 0;
var matches = $(field).val().match(/\s/g);
if(matches) {
number = matches.length;
}
if (globalWordCount != number) {
globalWordCount = number
$("#submit_button").click();
}
}
// Enable button if some text entered
$(document).ready(function(){
$('.btn').attr('disabled',true);
$('#review_id').keyup(function(){
if($(this).val().length !=0){
$('.btn').attr('disabled', false);
}
else
{
$('.btn').attr('disabled', true);
}
})
});
$('.review_class').each(function() {
var input = '#' + this.id;
var count = input + '_count';
$(count).show();
word_count(input, count);
$(this).keyup(function() { word_count(input, count) });
});
// Attach a submit handler to the form
$( "#analyzeForm" ).submit(function( event ) {
// Stop form from submitting normally
event.preventDefault();
// Get some values from elements on the page:
var $form = $( this ),
//term = $form.find( "input[name='review_name']" ).val(),
text = $form.find( "textarea" ).val(),
url = $form.attr( "action" );
//console.log(text, "text");
// Send the data using post
var posting = $.post( url, { review_name: text } );
// Put the results in a div
posting.done(function( data ) {
var indicator = ('Negative' === data.prediction) ? 100 - data.confidence : data.confidence;
gauge.trigger('updateGauge', indicator);
document.getElementById("result").innerHTML = data.prediction + " with confidence of " + data.confidence + "%";
$(btn).buttonLoader('stop');
console.log("Button click stop");
});
});
</script>
</div>
<div class='footer'>
<p><br><br><br><br><br><br><a href="https://github.com/nikhilno1/nlp_projects">Code on GitHub</a>
<br><i>Powered by Hugging Face's awesome pytorch-transformers library</i></p>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="../static/component-spinners.js"></script>
</body>
</html>
Can you please let me know how to fix this? Thank you.