2017-04-02 160 views
0

我正在開發一個帶JavaScript的聲音庫。我創建了sound.js文件,並將此文件包含在test.php文件中。 sound.js文件包含一些功能。我的sound.js文件是這樣的:外部JavaScript文件

var soundPath = './sound/'; 

/* 
* Sounds Path 
*/ 
var phoneNumberSound  = ['0.wav', '1.wav', '2.wav', '3.wav', '4.wav', '5.wav', '6.wav', '7.wav', '8.wav', '9.wav']; 
// var phoneCallSound   = null; 

/* 
* Play Phone Number's Sound 
*/ 
function playPhoneNumberSound (phoneNumber) 
{ 
    phoneNumber = parseInt(phoneNumber); 
    if (phoneNumber.length > 1){ 
     var phoneNumber = phoneNumber.substring(phoneNumber.length - 1); 
     new Audio(soundPath + phoneNumberSound[phoneNumber-1]).play(); 
    } else { 
     new Audio(soundPath + phoneNumberSound[phoneNumber-1]).play(); 
    } 
} 

/* 
* Coming Phone Incoming Sound 
*/ 
function playPhoneIncomingSound() 
{ 
    document.writeln('Test this func'); 
} 

在test.php中,我調用了上面的函數。第一個功能正在工作,但第二個功能或第三個或其他...不起作用。瀏覽器錯誤的結果是這樣的:

Uncaught ReferenceError: playPhoneIncomingSound is not defined 

我test.php的文件是這樣的:

<script src="lib/jquery.js"></script> 
<script src="lib/sound.js"></script> 

<input type="text" id="phoneNumber" /> 
<button id="playSound">Play Sound</button> 

<script type="text/javascript"> 

    $(document).ready(function() 
    { 
     $('#playSound').click(function() 
     { 
      playPhoneIncomingSound(); 
      // var phoneNumber = $('#phoneNumber').val(); 
      // playPhoneNumberSound(phoneNumber); 
     }); 
    }); 
</script> 

爲什麼?爲什麼會發生此錯誤?我的錯誤在哪裏?

謝謝!

編輯: 這個關於Chrome的錯誤。當我清除瀏覽數據時,這個錯誤會丟失。在火箱裏,我不喜歡這個錯誤!

回答

0

爲什麼不使用onclick調用playPhoneIncomingSound()

var soundPath = './sound/'; 
 

 
/* 
 
* Sounds Path 
 
*/ 
 
var phoneNumberSound = ['0.wav', '1.wav', '2.wav', '3.wav', '4.wav', '5.wav', '6.wav', '7.wav', '8.wav', '9.wav']; 
 
// var phoneCallSound   = null; 
 

 
/* 
 
* Play Phone Number's Sound 
 
*/ 
 
function playPhoneNumberSound(phoneNumber) { 
 
    phoneNumber = parseInt(phoneNumber); 
 
    if (phoneNumber.length > 1) { 
 
    var phoneNumber = phoneNumber.substring(phoneNumber.length - 1); 
 
    new Audio(soundPath + phoneNumberSound[phoneNumber - 1]).play(); 
 
    } else { 
 
    new Audio(soundPath + phoneNumberSound[phoneNumber - 1]).play(); 
 
    } 
 
} 
 

 
/* 
 
* Coming Phone Incoming Sound 
 
*/ 
 
function playPhoneIncomingSound() { 
 
    document.writeln('Test this func'); 
 
}
<button id="playSound" onclick="playPhoneIncomingSound()">Play Sound</button>

在你test.php的代碼中,playPhoneIncomingSound();試圖找到腳本的代碼的功能,而不是在sound.js

+0

我以前試過,但它不」工作。問題在於Chrome。多謝,夥計! – jsawyer

+0

鉻的問題是什麼?控制檯中有任何錯誤? –

+0

鉻高速緩存的此錯誤。 – jsawyer