You are misunderstanding use of addEventListener().
The condition in your checkIf()
is returning instantly. Thus your code is
- starting interval timer on invoking
strt()
- adding click handler as early as interval timer is elapsing for the first time
- but also it is instantly stopping interval time er and invoking
startDoing()
then.
Use it like that instead:
var intrvl;
var clicked = false;
function strt() {
//and set interval
intrvl = setInterval(checkIf,3000);
}
function checkIf() {
if ( clicked ) {
clearInterval( intrvl );
startDoing();
}
}
c.addEventListener('click', function() {
clicked = true;
} );
This code is instantly registering click handler. It is starting interval timer as soon as strt() is invoked. As soon as mouse has been clicked, the variable clicked is set true to mark this event. Next time your interval timer is triggering it is recognizing preceding click due to checking that variable and starting whatever you want to start.