I am encountering an issue with my 'highlightBookmark' function that is supposed to change the background color of a list item after 2 seconds. However, it is not working as expected! The background color only changes when the function is called by a click event and not automatically after the specified time has passed.
Below is the code snippet:
Controller.js
//markers
$scope.markers = [
{
time: 9.5,
text: "Bookmark 1",
class: false
},
{
time: 106,
text: "Bookmark 2",
class: false
}
]
$scope.currentBookmark = -1;
function highlightBookmark(index) {
$scope.markers[index].class = true;
}
var interval = setInterval(checkTime, 100);
function checkTime(){
if(Math.floor(player.currentTime()) == 2){
highlightBookmark(1)
clearInterval(interval);
}
}
$scope.jumpTo = function (index) {
highlightBookmark(index);
}
The 'highlightBookmark' function takes an integer as input, finds the object at that position, and updates the 'class' property to true. For example, passing the number 1 to the function will update the 'class' property of the object at index 2 to true.
Although the 'highlightBookmark' function is being called after 2 seconds, the class does not get updated, resulting in no change in background color.
When I call the function using a click event, it works perfectly.
HTML file
<ul id="bookmarkList">
<li ng-repeat="bookmark in markers" ng-class="{'active': bookmark.class}" ng-click="jumpTo($index)">{{bookmark.text}}</li>
</ul>
The list item in the HTML file has the ng-class property that should be changed after 2 seconds.
You can view a similar code on Codepen where a button's color changes upon click but doesn't change after setTimeout, even though the method is called.
https://codepen.io/Octtavius/pen/wgzORv
I would appreciate any help in resolving this simple issue.