In: Computer Science
Why does the percent go beyond 100% using the progressbar?
****************************************
timer.js
**************************************
var timer = undefined;
$(document).ready(function() {
$("#start_timer").click(
function () {
var totalTime =
$("#time").val();
var interval =
$("#interval").val();
var isValid =
true;
// validate the
time
if (totalTime ==
"") {
$("#time_error").text("This field is required.");
isValid = false;
} else if
(isNaN(totalTime)) {
$("#time_error").text("Time must be a number.");
isValid = false;
} else {
$("#time_error").text("");
}
// validate the
interval
if (interval ==
"") {
$("#interval_error").text("This field is required.");
isValid = false;
} else if
(isNaN(interval)) {
$("#interval_error").text("Interval must be a number.");
isValid = false;
} else {
$("#interval_error").text("");
}
if (isValid)
{
console.log('Valid. Hence starting');
totalTime = totalTime * 1000;
interval = interval * 1000;
var elapsedTime = 0;
var progressbar = $( "#progressbar" );
var progressLabel = $( ".progress-label" );
progressbar.progressbar({
value: 0,
max: 100
});
function progress() {
elapsedTime += interval;
var perc = 100.0 * elapsedTime/totalTime;
console.log('making progress ' + perc)
progressLabel.text(perc.toFixed(0) + '%');
if(perc > 100) {
perc = 100;
progressbar.progressbar( "value", perc );
$("#start_timer").removeAttr('disabled');
} else {
progressbar.progressbar( "value", perc );
setTimeout(progress, interval);
}
}
progressLabel.text('0%');
setTimeout( progress, interval);
$("#start_timer").attr('disabled','disabled');
}
}
);
$("#totalTime").focus();
});
Your code is working perfectly dear. But you have left two things. I have modifyied them. :)
Here is the modified and perfectly working code for your jQuery progressbar.
jQuery progressbar.html
<!DOCTYPE html>
<html>
<head>
<title>Progress Bar</title>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="jquery-3.5.1.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<style type="text/css">
input{
padding: 10px;
border: 1px solid gray;
font-size: 15px;
}
</style>
</head>
<body>
<div>
<form>
<input id="time" type="text" placeholder="time">
<p id="time_error"></p>
<input id="interval" type="text" placeholder="interval">
<p id="interval_error"></p>
<input type="button" value="start" id="start_timer">
</form>
<br><br>
<div id="progressbar"></div>
<div class="progress-label"></div>
</div>
<script type="text/javascript">
var timer = undefined;
$(document).ready(function() {
$("#start_timer").click(
function () {
var totalTime = $("#time").val();
var interval = $("#interval").val();
var isValid = true;
// validate the time
if (totalTime == "") {
$("#time_error").text("This field is required.");
isValid = false;
} else if (isNaN(totalTime)) {
$("#time_error").text("Time must be a number.");
isValid = false;
} else {
$("#time_error").text("");
}
// validate the interval
if (interval == "") {
$("#interval_error").text("This field is required.");
isValid = false;
} else if (isNaN(interval)) {
$("#interval_error").text("Interval must be a number.");
isValid = false;
} else {
$("#interval_error").text("");
}
if (isValid) {
console.log('Valid. Hence starting');
totalTime = totalTime * 1000;
interval = interval * 1000;
var elapsedTime = 0;
var progressbar = $( "#progressbar" );
var progressLabel = $( ".progress-label" );
progressbar.progressbar({
value: 0,
max: 100
});
function progress() {
elapsedTime += interval;
var perc = 100.0 * elapsedTime/totalTime;
console.log('making progress ' + perc)
progressLabel.text(perc.toFixed(0) + '%');
//check the perc value is greater than or equal to 100
if(perc >= 100) {
perc = 100;
progressbar.progressbar( "value", perc );
$("#start_timer").removeAttr('disabled');
//display the updated value of perc in progressLabel
progressLabel.text(perc.toFixed(0) + '%');
} else {
progressbar.progressbar( "value", perc );
setTimeout(progress, interval);
}
}
progressLabel.text('0%');
setTimeout( progress, interval);
$("#start_timer").attr('disabled','disabled');
}
}
);
$("#totalTime").focus();
});
</script>
</body>
</html>
Output:
--------------------------------------
jQuery code only: (timer.js)
var timer = undefined;
$(document).ready(function() {
$("#start_timer").click(
function () {
var totalTime = $("#time").val();
var interval = $("#interval").val();
var isValid = true;
// validate the time
if (totalTime == "") {
$("#time_error").text("This field is required.");
isValid = false;
} else if (isNaN(totalTime)) {
$("#time_error").text("Time must be a number.");
isValid = false;
} else {
$("#time_error").text("");
}
// validate the interval
if (interval == "") {
$("#interval_error").text("This field is required.");
isValid = false;
} else if (isNaN(interval)) {
$("#interval_error").text("Interval must be a number.");
isValid = false;
} else {
$("#interval_error").text("");
}
if (isValid) {
console.log('Valid. Hence starting');
totalTime = totalTime * 1000;
interval = interval * 1000;
var elapsedTime = 0;
var progressbar = $( "#progressbar" );
var progressLabel = $( ".progress-label" );
progressbar.progressbar({
value: 0,
max: 100
});
function progress() {
elapsedTime += interval;
var perc = 100.0 * elapsedTime/totalTime;
console.log('making progress ' + perc)
progressLabel.text(perc.toFixed(0) + '%');
//check the perc value is greater than or equal to 100
if(perc >= 100) {
perc = 100;
progressbar.progressbar( "value", perc );
$("#start_timer").removeAttr('disabled');
//display the updated value of perc in progressLabel
progressLabel.text(perc.toFixed(0) + '%');
} else {
progressbar.progressbar( "value", perc );
setTimeout(progress, interval);
}
}
progressLabel.text('0%');
setTimeout( progress, interval);
$("#start_timer").attr('disabled','disabled');
}
}
);
$("#totalTime").focus();
});
Hope you like it.
If you have any doubts ask me in comment section.
If you like my work, please give me a like and feedback. That helps me a lot. Thank you.
All the best.