我一直在我的网站上使用它作为里程表 http://github.hubspot.com/odometer/,它很棒,但我似乎无法让它能够处理按钮onclick动作。它只会在加载页面时起作用。我试图添加到其他功能,如一个简单的window.alert警报将工作,但它不会启动setTimeOUt
这是我的最新代码:
<script>
function playSound() {
setTimeout(function(){
var audio = new Audio('sounds/machine-002-short.mp3');
var audio2 = new Audio('sounds/coin-drop-4.mp3');
audio.play();
audio.volume = 0.65;
audio.currentTime = 0.90;
audio2.pause();
audio.addEventListener("ended", function() {
audio2.play();
audio2.volume = 1.5;
}); // event listener
$('.odometer').html(<?php echo $Display;?>);
}, 1000);
}
</script>
和按钮:
<button id="submit" type="submit" type="button" name="roll" onclick="playSound();">Start</button>
如果我只是添加一个winow.alert它只是没有这个设置的时间,我在JS非常缺乏经验,所以我相信它是简单的
答案 0 :(得分:1)
function getContent() {
$.get($("#form1").prop("action", {data:$("#odometer").val()},function(data) {
$('#odometer').val(data); // here we replace the $Display with what is returned
});
}
function playSound() {
setTimeout(function() {
var audio = new Audio('sounds/machine-002-short.mp3');
var audio2 = new Audio('sounds/coin-drop-4.mp3');
audio.play();
audio.volume = 0.65;
audio.currentTime = 0.90;
audio2.pause();
audio.addEventListener("ended", function() {
audio2.play();
audio2.volume = 1.5;
getContent();
}); // event listener
$('#odometer').val(<?php echo $Display;?>);
}, 1000);
}
<form id="form1" action="somefile.php">
<textarea id="odometer"></textarea>
<button type="button" id="but">Start</button>
</form>