Reputation: 1396
I'm having trouble updating the value of a variable q
with a javascript function. I have a timer that starts when "play" is pressed, and then continuously fires the current demand function
$(document).ready(function(){
$("#main_video").on(
"timeupdate",
function(event){
currentDemand(this.currentTime);
});
});
// Initiate Demand
var q = 0;
function currentDemand(currentTime){
console.log(currentTime);
if ( 0 <= currentTime < 10) {
q === 0;
}
else if ( 10 <= currentTime < 12.5 || 57.5 <= 60) {
q === 1;
}
else if ( 12.5 <= currentTime < 17.5 || 55 <= currentTime < 57.5) {
q === 2;
}
else if ( 17.5 <= currentTime < 20 || 50 <= currentTime < 55) {
q === 3;
}
else if (20 <= currentTime < 25 || 47.5 <= currentTime < 50) {
q === 4;
}
else if ( 25 <= currentTime < 27.5 || 45 <= currentTime < 47.5 ) {
q === 5;
}
else if ( 27.5 <= currentTime < 32.5 || 42500 <= currentTime < 45 ) {
q === 6;
}
else if ( 32.5 <= currentTime < 35 || 40 <= currentTime < 42.5 ) {
q === 7;
}
else if ( 45 <= currentTime < 47.5 ) {
q === 8;
}
console.log(q);
$("#currentDemand").text(q);
}
currentTime
is logging correctly at values that should put it in the intervals, but q
is not being set. It probably has something to do with the scope, I just can't figure out how to fix it so that q
is updated correctly and put into the #currentDemand
div.
How can I set q
to update?
Upvotes: 0
Views: 47
Reputation: 780724
This is not the correct way to test if currentTime
is between two values:
else if ( 10 <= currentTime < 12.5 || 57.5 <= 60) {
It's parsed as if you'd written:
else if ( ((10 <= currentTime) < 12.5) || (57.5 <= 60)) {
Since 57.5
is always less than 60
, the entire expression will always be true. But 10 <= currentTime < 12.5
is also always true, because 10 <= currentTime
is either true
or false
, and they're converted to 1
or 0
when comparing with another number. And both 1 and 0 are less than 12.5.
It should be written as:
else if ( (10 <= currentTime && currentTime < 12.5) || (57.5 <= currentTime && currentTime < 60) ) {
Upvotes: 2
Reputation: 67505
You should use one equal =
for assignment purpose, the both double ==
and triple ===
equals are used for comparaison.
Also you should separate your condition to looks like :
if ( currentTime >= 0 && currentTime < 10) {
q = 0;
}
else if ( currentTime >= 10 && currentTime < 12.5 || 57.5 <= 60) {
//__________________________________________________^__________^
//this condition should be removed because it return always true
q = 1;
}
else
.....
Hope this helps.
Upvotes: 1
Reputation: 762
To assign a value, you must only use one =
if you use ==
or ===
are comparative. More info here:
Upvotes: -1
Reputation: 126
q = whatevernumber;
not q === whatevernumber
, which returns a boolean
Upvotes: 0