JavaScript set timeout loop return value
问题 I have this function which pings my server for a specific change in data: function connected() { $.ajax({ success : function(d) { if (d.success) { // return data } else { setTimeout(connected, 200); } } }); } (Obviously the code has been stripped to the bare minimum to make my intentions clearer) What I want to do is return a bool true when it gets found, so I can go if (connected()) { // process data } (obviously the data is stored in a global variable.) Is there any way to achieve this?