Jquery setTimeout()函数触发错误

Jquery setTimeout()函数触发错误,jquery,recursion,settimeout,Jquery,Recursion,Settimeout,希望每5秒运行一次函数a,但仅在对完全加载的文档执行主函数后3秒 jQuery(document).ready(function() { setTimeout(function() { var audioElement = document.createElement('audio'); audioElement.setAttribute('src', 'engine_start.mp3'); audioElement.setAttribute('autoplay',

希望每5秒运行一次函数
a
,但仅在对完全加载的文档执行主函数后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);   
}

如果要在每5秒钟后运行函数a,请使用
setInterval()
而不是
setTimout()


您必须使用
setInterval()
而不是
setTimeout()
,它将在5秒后反复运行

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

您遇到了什么错误???运行正常,但不是在循环中运行。/。。。如何再次运行函数A n为什么一直将图像源更改为同一图像?它将再次运行n次或1次?它将再次运行n次@Shafqat
setInterval(function(){ 
     $('.light').attr("src","images/light-on.png");
}, 5000);