setTimeout()函数触发错误

时间:2015-02-02 06:27:12

标签: jquery recursion settimeout

希望每5秒运行一次函数a,但在完全加载的文档上执行main函数后仅3秒。

jQuery(document).ready(function() {
setTimeout(function() {

    var audioElement = document.createElement('audio');
    audioElement.setAttribute('src', 'engine_start.mp3');
    audioElement.setAttribute('autoplay', 'autoplay');
    //audioElement.load()

    $.get();

    audioElement.addEventListener("load", function() {
        audioElement.play();
    }, true);

   $('.car').click(function() {
        audioElement.play();
    });


}, 3000);


function a {
setTimeout(function() { 
$('.light').attr("src","images/light-on.png");  
}, 2500);   
}

2 个答案:

答案 0 :(得分:0)

如果您希望每5秒钟后运行Function a,请使用setInterval()代替setTimout()

setInterval(function(){ a(); }, 5000);

function a {
  $('.light').attr("src","images/light-on.png");
}

答案 1 :(得分:0)

您必须使用setInterval()代替setTimeout(),这将在5秒后反复运行。

setInterval(function(){ 
     $('.light').attr("src","images/light-on.png");
}, 5000);