2013-09-27 2 views
3

내 문제는 몇 초 후에 PAUSE btn &을 누르면 문제가 발생합니다. 재생 버튼을 누른 다음 타이머가 처음부터 시작됩니다. '타이머는 현재 일시 중지 시간, 사용자가 재생 버튼을 클릭 한 다음 일시 중지 버튼을 클릭 한 경우.jquery를 사용하여 타이머를 다시 시작하는 방법

이 문제를 해결하는 데 도움을 주시기 바랍니다. 지난 2 일이 문제를 해결하기 위해 노력하고 있습니다. 하지만 여전히 내 애플 리케이션 요구 사항에 따라 해결되지.

function myApp() { 
    this.paused = false; 
    this.paused = true // set pause to true (stop) 
    this.isactive = false; // countdown is active 
    return this.observer(); // call and return the "observer" method of the "myApp" class 
} 

myApp.prototype.observer = function() { // observer method 
    var self = this; // set this to self (this is the current instance of this class) 

    $('#btn_start').on('click', function(event){ // where an user click on "btn_start" 
    event.preventDefault(); 
    self.play('mp3'); // call the play method and store the state in a public var 
    self.countdown(30, self.onEnd); // 30 is the audio duration - second parameter is the callback when the audio is finished 
    self.isactive = true; 
    }); 
    return this; // return this instance 
}; 

myApp.prototype.play = function() { // play method 
    var song = document.getElementById('audiosource'); 
    if (this.paused === true) 
    { 
    console.log('play song'); 
    song.play(); 
    this.paused = false; 
    } 
    else 
    { 
    console.log('pause song'); 
    song.pause(); 
    this.paused = true; 
    } 
    return this; 
}; 

myApp.prototype.countdown = function(duration, callback) { // countdown method 
    var self = this, // class instance 
     countdown = null, // countdown 
     ctx = null; // setIntervall clearer 
     timer = duration; // timer 

    if (this.isactive === true) // if this method yet called 
    { 
    return false; 
    } 
    countdown = function() { 
     console.log('start countdown:' + self.paused); 
    if (timer === 0) 
    { 
     clearInterval(ctx); 
     callback.call(this); 
     return false; 
    } 
    if (self.paused === false) // if not in pause 
    { 
     timer -= 1; 
     console.log(timer); 
     $('#timer > span').html(timer); 
    } 
    }; 
    ctx = setInterval(countdown, 1000); 
}; 

myApp.prototype.onEnd = function() { 
    // when the audio is finish.. 
alert ('end of the song'); 
}; 

; $(function() { 
    new myApp(); 
}); 
+1

질문에 코드를 입력하지 않았습니다. –

+0

@KevinB jquery 코드로 내 질문을 업데이트했습니다. 또한 필자는 바이올린에 전체 코드를 포함 시켰습니다. (http://jsfiddle.net/XYevE/1/) – beginner

+0

https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_HTML5_audio_and_video?redirectlocale=en-US&redirectslug=Web%2FHTML%2FUsing_HTML5_audio_and_video –

답변

1

I 앱을 다르게

<div id="app"> 
    <div id="timer">click play</div> 
    <a href="#" id="btn_start">PLAY</a> 
</div> 

<audio id="audiosource"> 
    <source src="http://www.virtuagym.com/audio/en/get_ready.mp3" type="audio/mpeg" /> 
    <source src="http://www.soundjay.com/button/button-1.wav" type="audio/wav" /> 
</audio> 


<audio id="a_1" > 
    <source src="http://www.virtuagym.com/audio/en/1.ogg" type="audio/ogg" /> 
    <source src="http://www.virtuagym.com/audio/en/1.mp3" type="audio/mp3" /> 
</audio> 
<audio id="a_2" > 
    <source src="http://www.virtuagym.com/audio/audio/en/2.ogg" type="audio/ogg" /> 
    <source src="http://www.virtuagym.com/audio/audio/en/2.mp3" type="audio/mp3" /> 
</audio> 
<audio id="a_3" > 
    <source src="http://www.virtuagym.com/audio/audio/en/3.ogg" type="audio/ogg" /> 
    <source src="http://www.virtuagym.com/audio/audio/en/3.mp3" type="audio/mp3" /> 
</audio> 
<audio id="a_4" > 
    <source src="http://www.virtuagym.com/audio/audio/en/4.ogg" type="audio/ogg" /> 
    <source src="http://www.virtuagym.com/audio/audio/en/4.mp3" type="audio/mp3" /> 
</audio> 
<audio id="a_5" > 
    <source src="http://www.virtuagym.com/audio/audio/en/5.ogg" type="audio/ogg" /> 
    <source src="http://www.virtuagym.com/audio/en/5.mp3" type="audio/mp3" /> 
</audio> 

JS을 구성합니다 :

(function ($) { 
    // function to play sounds, calls `done` when sounds are finished to play 
    // here you should adjust timings for your audio 
    var playGetReady = function (done) { 
     var ids = ['audiosource', 'a_5', 'a_4', 'a_3', 'a_2', 'a_1'], 
      playNext = function() { 
       var id = ids.shift(); 
       document.getElementById(id).play(); 
       if (ids.length) { 
        setTimeout(playNext, 1000); 
       } else { 
        done(); 
       } 
      }; 
     playNext(); 
    }; 

    // constructor 
    function App($el, startFrom) { 
     this.$el = $el; // root element 
     this.$timer = this.$('#timer'); // find element to render countdown to 
     this.$button = this.$('#btn_start'); // play/pause button 
     // $.proxy(fn, ctx); makes `ctx` to be referenced by `this` inside `fn` 
     // sets handler on button 
     this.$button.click($.proxy(this.buttonHandler, this)); 
     // sets value to start countdown 
     this.set(startFrom); 
     // we're not running yet 
     this.intervalHandle = null; 
    }; 

    // look for elements inside of root element 
    App.prototype.$ = function() { 
     return this.$el.find.apply(this.$el, arguments); 
    }; 

    // called on play/pause button click 
    App.prototype.buttonHandler = function (e) { 
     e.preventDefault(); // prevent anchor default action 
     this.toggle(); // toggle play/pause 
    }; 

    App.prototype.start = function() { 
     var self = this; 
     playGetReady(function() { // play get ready sound 
      // start countdown 
      self.intervalHandle = setInterval($.proxy(self.tick, self), 1000); 
      // change button text to PAUSE 
      self.$button.text('PAUSE'); 
     }); 
    }; 

    App.prototype.stop = function() { 
     // stop countdown 
     clearInterval(this.intervalHandle); 
     // set `intervalHandle` to null to be able to check whether 
     // countdown is running or not 
     this.intervalHandle = null; 
     // change button text to PLAY 
     this.$button.text('PLAY'); 
    }; 

    App.prototype.toggle = function() { 
     // if running 
     if (this.intervalHandle) { 
      // then stop 
      this.stop(); 
     // if not 
     } else { 
      // then start 
      this.start(); 
     } 
    }; 

    // sets new value for countdown 
    App.prototype.set = function (timeLeft) { 
     this.counter = timeLeft; 
    }; 

    // called every second to update counter, rerender, call `end` callback 
    // and play sound 
    App.prototype.tick = function() { 
     // update countdown 
     this.counter -= 1; 
     // push new countdown to page 
     this.render(); 
     if (this.counter === 0) { // if countdown is finished 
      this.stop(); // stop decreasing it 
      this.end(); // end callback, ask for new value or terminate 
     } 
    }; 

    // pushed countdown to page 
    App.prototype.render = function() { 
     this.$timer.text(this.counter); 
    }; 

    // on end callback 
    App.prototype.end = function() { 
     // ask for new countdown 
     var text = prompt('end of the first exercise, NOw lets play the second exercise for your with another by loading its time'); 
     if (text) { // if user entered any 
      var next = Number(text); // convert it to number 
      this.set(next); // set new countdown value 
      this.render(); // push changes to page 
      this.start(); // start countdown 
     } else { // if user entered nothing 
      // do nothing 
      this.$timer.text('You\'re done'); // tell user he finished 
     } 
    }; 

    $(function() { 
     var app = new App($('#app'), 5); // create app 
    }); 
})(jQuery); 

주, 당신은 당신의 소리에 따라 playGetReady에 타이밍을 조정해야합니다. 또한 a_5a_1 만로드 중입니다.

도움이 될 것입니다. 나는 또한 튜토리얼이나 자바 스크립트에 대한 책을 읽는 것이 좋습니다.

jsFiddle

+0

바이올린 코드에 '음소거/음소거 해제'기능을 구현하는 로직을 제공해 주실 수 있습니까? 여기 http://jsfiddle.net/rhy5K/25/ 시도했는데 작동하지 않습니다. – beginner

+0

'this.muted'를 볼 수 있지만 사운드 재생을 막기 위해 사용하지 않습니다. 'start' 메소드에서'this.newTimer'처럼 음소거가되어 있는지 확인해야합니다. – zaquest

+0

어려운 사람.나는 지켰지 만 그 소리는 소리가 나지 않는다. 링크 토글 볼륨을 클릭하면 발생하지 않는다. – beginner

0

시간 표시기를 어딘가에 저장해야하며 재생을 다시 누르면 시간 표시기에서 다시 시작됩니다. 방금 ​​timeleft var를 추가했습니다.

function myApp() { 

this.timeleft = 30; 

    this.paused = false; 
    this.paused = true; // set pause to true (stop) 
    this.isactive = false; // countdown is active 
    return this.observer(); // call and return the "observer" method of the "myApp" class 
} 

myApp.prototype.observer = function() { // observer method 
    var self = this; // set this to self (this is the current instance of this class) 

    $('#btn_start').on('click', function(event){ // where an user click on "btn_start" 
    event.preventDefault(); 
    self.play('mp3'); // call the play method and store the state in a public var 
    self.countdown(self.timeleft, self.onEnd); // 30 is the audio duration - second parameter is the callback when the audio is finished 
    self.isactive = true; 
    }); 
    return this; // return this instance 
}; 

myApp.prototype.play = function() { // play method 
    var song = document.getElementById('audiosource'); 
    if (this.paused === true) 
    { 
    console.log('play song'); 
    song.play(); 
    this.paused = false; 
    } 
    else 
    { 
    console.log('pause song'); 
    song.pause(); 
    this.paused = true; 
    } 
    return this; 
}; 

myApp.prototype.countdown = function(duration, callback) { // countdown method 
    var self = this, // class instance 
     countdown = null, // countdown 
     ctx = null; // setIntervall clearer 
     timer = duration; // timer 

    if (this.isactive === true) // if this method yet called 
    { 
    return false; 
    } 
    countdown = function() { 
     console.log('start countdown:' + self.paused); 
    if (timer === 0) 
    { 
     clearInterval(ctx); 
     callback.call(this); 
     return false; 
    } 
    if (self.paused === false) // if not in pause 
    { 
     timer -= 1; 
     console.log(timer); 
     self.timeleft = timer; 

     $('#timer > span').html(timer); 
    } 
    }; 
    ctx = setInterval(countdown, 1000); 
}; 

myApp.prototype.onEnd = function() { 
    // when the audio is finish.. 
alert ('end of the song'); 
}; 

;$(function() { 
new myApp(); 
}); 
+0

나는 또한 '카운트 다운'에'duration '을 전달하고'카운트 다운'에서'타이머 '를 제거하지 않을 것을 제안합니다. 또한'ctx = null; 대신'ctx = null '을 원한 것으로 보인다. http://jsfiddle.net/rhy5K/ – zaquest

+0

@ user2732367 글쎄,이 코드에서 더 많은 작업을해야한다고 생각한다. 그렇지만이 코드를 사용하면된다. 사용자가 원하는 값으로 타이머를 설정할 수있게하려면 [this] (http://jsfiddle.net/rhy5K/1/)와 비슷한 'timer'에 대한 setter 함수를 작성할 수 있습니다. – zaquest

+0

@ user2732367 일부 부분을 정리해야한다는 뜻입니다. 예를 들어'this.paused = false; this.paused = true'를 반환합니다. 먼저 두 번째 과제가 끝날 때 두 번째 과제를 사용해야합니다. 또한 이미 말했듯이, 부수적으로'카운트 다운 '에 전역'timer' 변수를 도입하고 있습니다. 그러나 코드가 올바르게 작동하므로 반드시 사용해야합니다. – zaquest

관련 문제