Note that there are some explanatory texts on larger screens.

plurals
  1. POwaiting for DOM src attribute to change
    primarykey
    data
    text
    <p>So when I click on the tutorial navigation menu it fires the ng-click="loadNewTutorial($index)" function, everything works ok tutorialNumber gets updated by the index number and that in turn changes the URL of the current video. The only problem is that video.load()/video.play() apparently fires before that change occurs in the DOM because the video is always 1 click behind. What I mean is I can click the menu item to load the second tutorial video but it will will load the first, then the NEXT time I click it will load the second, then if I click the first again it will be stuck on the second until I click the first again. So the current video doesnt load the correct URL in time and is always 1 click behind! How can I make the video.load() function wait until the videos src attribute in the DOM has been changed before it fires?</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html ng-app="Tutorials"&gt; &lt;head&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js"&gt;&lt;/script&gt; &lt;script src="./functions.js"&gt;&lt;/script&gt; &lt;script src="./tutorials.js"&gt;&lt;/script&gt; &lt;script src="http://code.jquery.com/jquery-1.9.1.min.js"&gt;&lt;/script&gt; &lt;script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="./script-ng.js"&gt;&lt;/script&gt; &lt;link rel="stylesheet" href="main.css"&gt;&lt;/link&gt; &lt;/head&gt; &lt;body ng-controller="getAnswers"&gt; &lt;div id="sidebar_left"&gt;&lt;div id="header"&gt;&lt;/div&gt; &lt;ul id="nav" ng-repeat="section in sets"&gt; &lt;li class="section_title {{section.active}}" &gt; {{section.name}} &lt;/li&gt; &lt;ul&gt;&lt;li class="tutorial_title {{tutorial.active}}" ng-click="loadNewTutorial({{$index}})" ng-repeat="tutorial in section.tutorials"&gt;{{tutorial.name}}&lt;/li&gt;&lt;/ul&gt; &lt;/ul&gt;&lt;span&gt;{{score}}&lt;/span&gt; &lt;/div&gt; &lt;div id="container"&gt; &lt;video id="video" controls&gt; &lt;source src="{{videoURLs[tutorialNumber]}}.mp4"&gt;&lt;/source&gt; &lt;source src="{{videoURLs[tutorialNumber]}}.webm"&gt;&lt;/source&gt; Your browser does not support the video tag. &lt;/video&gt; &lt;br /&gt; &lt;button id="reset"&gt;Replay&lt;/button&gt; &lt;br /&gt; &lt;div id="pannel"&gt; &lt;div id="base"&gt;{{verbs.base}}&lt;/div&gt; &lt;ul class="answers"&gt; &lt;li ng-click="checkAnswer(answer)" ng-repeat="answer in verbs.conjugations" class="{{answer.colorReveal}} answer {{answer.correct}}" id="answer{{$index}}"&gt;{{answer.text}}&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;br /&gt; &lt;br /&gt; &lt;br /&gt; &lt;div id="warning"&gt;&lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; angular.module('Tutorials', ['functions', 'tutorials']).controller('getAnswers', function ($scope, $element) { $scope.sectionNumber = 0; $scope.tutorialNumber = 0; $scope.loadNewVerbs = function () { $scope.verbs = conjugate(conjugationsets[$scope.tutorialNumber][$scope.questionNumber]); $scope.correct = $scope.verbs.conjugations[0].text; fisherYates($scope.verbs.conjugations); }; $scope.checkAnswer = function (answer) { if (false) {//wait for tutorial to pause $("#warning").text("Not yet!").fadeIn(300).delay(1400).fadeOut(300); return; }; answer.colorReveal = "reveal-color"; if (answer.text === $scope.correct) { //if correct skip to congratulations $scope.questionNumber++; if(sectionNumber === 0 &amp;&amp; $scope.tutorialNumber ===0){ //if first video play congratulations etc congrats(); } setTimeout(function () { $scope.loadNewVerbs(); $scope.$digest(); }, 2000); } else { //if incorrect skip to try again msg if(sectionNumber === 0 &amp;&amp; $scope.tutorialNumber ===0){start(160.5); pause(163.8)} } }; $scope.sets = [{ active:"inactive", name: "Conjugation", tutorials: [ {active:"inactive",name: "ㅗ and ㅏ regular"}, {active:"inactive",name:"ㅜ, ㅓ and ㅣ regular"}, {active:"inactive",name:"ㅏ and ㅓ reductive"}, {active:"inactive",name: "ㅗ and ㅜ reductive"}, {active:"inactive",name: "ㅣ reductive"}] }, { active:"inactive", name: "Sentence_Building", tutorials: [ {active:"inactive",name:"Particles"}, {active:"inactive",name:"Word Order"}] }]; $scope.video = $("#video").get(0); $scope.videoURLs = ["ㅗㅏregular", "ㅜㅓㅣregular"]; $scope.loadNewTutorial = function (tut) { $scope.score = 0; $scope.questionNumber = 0; if(tut){ $scope.tutorialNumber = tut; } video.load(); video.play(); video.hasPlayed = true; $(video).bind('ended', endVideoHandler); $scope.loadNewVerbs(); }; $scope.loadNewTutorial(); var congrats = function(){ if ($scope.questionNumber === 1) {start(164.15); pause(166.8); $scope.score++; } else if ($scope.questionNumber === 2) {start(167.1); pause(170); $scope.score++; } else if ($scope.questionNumber === 3) { start(171.1); $scope.score ++;$scope.questionNumber = 0;$scope.tutorialNumber++; } } function endVideoHandler() { $(this).unbind('ended'); if (!this.hasPlayed) { return; } $scope.tutorialNumber++; $scope.$digest(); this.currentTime = 0; $scope.loadNewTutorial(); } }); var conjugationsets = [ ["작다", "놀다", "닦다"], ["웃다", "울다", "멀다"] ]; var sectionNumber = 0; var tutorialNumber = 0; var questionNumber = 0; $(function () { $("#nav").accordion({ heightStyle: "content", header: ".section_title" }); $("#reset").click(function () { currentTutorial.currentVideo.videoObject.currentTime = 0; currentTutorial.currentVideo.start(); }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload