Javascript bug: setTimeout is not always working

Hello all,

We have found out recently that JS setTimeout is not always working!
That is we have for example this setTimeout:

setTimeout(function () {try_conn (data.idPeer, stream); }, connect_time);

it works 70% of the time.
But in about 30% of times it does not fire at all!
Do you know how we can solve this bug of JS and make sure setTimeout runs
100% of the time?

Thank

More than that will be needed to diagnose the issue. At minimum, example code that exhibits the problem and allows us to experience the issue, will be wanted.

2 Likes

Paul,

Here is the whole related code:

function connectToNewUser (PeerId, stream) { 

    const call = myPeer.call(PeerId, stream); 

	setTimeout(function () {try_conn (PeerId, stream); }, conn_wait_time);

    // Add their video
    video_vis = document.createElement('video'); 

    call.on('stream', userVideoStream => {
		video_connection = 'on';
		clearTimeout(video_intel);
		close_div('vis_anim');
        addVideoStream(video_vis, userVideoStream);
    });


}

function try_conn (PeerId, stream) {

	if (video_connection == 'on') {
		return false;
	}

	++try_Count;

	if (try_Count == 30) {
		load_error(connection_error);
		return false;
	}

	conn_wait_time = 2000;
	connectToNewUser (PeerId, stream);

}

Thanks,
Dean

Thank you for what you provided but it doesn’t seem possible to diagnose your issue just from the code, as it seems that the timeout gets run every time.

Unless there’s some way for us to actually experience the problem ourself, guesswork is going to be involved.

If the setTimeout doesn’t seem to fire on 30% of the cases, it should be possible to add logging to resolve the matter.

You can have a global setTimeoutCounter (for absolute clarity), so that you can log starting the setTimeout for that counter, then in the try_conn() function log each of the different ways that things get resolved with that counter, such as connection already on, load error, etc.

You could even add that info to an array so that processing the results is easier to interpret too.

I’ve put together a simple test of different connection situations, such as it working first off, having to wait three times before connecting, and not connecting at all, and the setTimeout seems to work well in all cases.

The browser console at https://jsfiddle.net/pmw57/32cvtau5/1/ shows all of the results as things are being checked.

1 Like

Hello Paul,

Thanks for your reply.
But this JS fail/bug is there for sure.
FYI, it happens again 30% of the time, so impossible to give a URL to it.
So you can see it by singing up for Anoox free member account and having video chat.
You will see that in 30 of the time this does not run:

setTimeout(function () {try_conn (PeerId, stream); }, conn_wait_time);

when the code below it, this does not run:

 call.on('stream', userVideoStream => {
		console.log('waiting in Conn: ');
		video_connection = 'on';
		clearTimeout(video_intel);
		close_div('vis_anim');
        addVideoStream(video_vis, userVideoStream);
    });

As it seems that this issue is not from JavaScript syntax or use or setTimeout, and is instead due to vagaries of video communication, I’ll leave this for others more expert in troubleshooting the ways of video than I.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.