1

According to this thread: how many javascript setTimeout/ setInterval call can be set simultaneously in one page? it is possible to have multiple setIntervals at the sametime.

However, is it possible to have a setInterval and setTimeout at the sametime?

This is not working...

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script> 
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script>
    $(document).ready(function() {
        init();
    }); 

    function init() {
        $('#startbutton').on('click', handleClick);
    }
</script>   
</head>
<body>
<script>

var playBeep = function () {
var snd = new Audio("beep-7.wav");
snd.play();
}

var handleClick = function () {
    var interval1 = setInterval(updateDisplay, 1); 
    makeIntervals([1,2,3], playBeep);
}

var makeIntervals = function(timeList,callback){
intervals = []
for(i in timeList){
    intervals.push(setTimeout(callback,timeList[i]))
}
return intervals
}

function updateDisplay() {
    var value = parseInt($('#timer').find('.value').text(), 10);
    value++;
    $('#timer').find('.value').text(value);
}

</script>
<button type="button" id="startbutton">Play Beep</button>
<P>
<div id="timer"><span class="value">0</span> ms</div>
<P>
Community
  • 1
  • 1
dublintech
  • 16,815
  • 29
  • 84
  • 115
  • did you try running them to see for yourself? – Kai Qing Nov 09 '12 at 01:05
  • You could have tried it with very little code – Esailija Nov 09 '12 at 01:05
  • "Not working" isn't a problem description. – Daedalus Nov 09 '12 at 01:07
  • @Kai Qing I added question accidentally before adding code. When I comment out the setInterval, the setTimeout works. When I commented out the setTimeout. the setInterval works. – dublintech Nov 09 '12 at 01:08
  • 1
    In a general sense it is definitely possible to have multiple timeouts and intervals at once. More specifically, when I put [your code in a fiddle](http://jsfiddle.net/KuaFZ/) it worked (using `console.log()` instead of the audio thing since I didn't have your audio file). Note though that each time the button is clicked you create another interval without clearing the previous one. Also, [the browser won't really do a 1ms interval](http://stackoverflow.com/questions/9647215/what-is-minimum-millisecond-value-of-settimeout). – nnnnnn Nov 09 '12 at 01:16

1 Answers1

0

Of course. However just make sure you don't have too many.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592