Make Uncaught TypeError not stop Javascript

Steve Tinsley
Steve Tinsley used Ask the Experts™
on
I have a script which is polling for new data every 10seconds and doing things with the data.
Is there a way that if the browsers incurs an Uncaught TypeError - that the polling just tries again rather than stopping the js?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2017

Commented:
Try with an if statement like this:
if((typeof data) === "undefined"){
//code here
}

Open in new window

Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
What is causing the error - maybe you should be addressing that.
Multitechnician
Top Expert 2014
Commented:
just put the code that throw the "Uncaught TypeError" in a try catch

setInterval(function() {
       try {
            // put the code here
       }
       catch(error){}
}, 10 * 1000);

Open in new window

Author

Commented:
What I was hoping for was a quick fix globally.... I know it's not good practice, but I havent got time to get it fully tested right now and I dont want the system to stop working.
leakim971Multitechnician
Top Expert 2014

Commented:
What I was hoping for was a quick fix globally.... I know it's not good practice, but I havent got time to get it fully tested right now and I dont want the system to stop working.

I well understood that, that's why my code is global and over your ajax code,

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial