Copy link to clipboard
Copied
Hello,
I've got a function, part of which attempts to communicate with an API at work (I've removed the name of the actual website to protect identity). Occasionally, the content management system we use goes down and is unresponsive, so I've put this into a try/catch and have an error dialog that pops up if it isn't responding.
function connectOuDlg() {
oldPidNum = activeDocument.name.substring(0,6)
imageType = activeDocument.name.substring(activeDocument.name.length - 6, activeDocument.name.length - 4)
try{
reply = "";
conn = new Socket();
conn.open ("***.com:80", "UTF-8")
conn.write ("GET ***.com/api/products/" + oldPidNum + "/for-studio?content-type=application/json HTTP/1.0\r\nHost:***.com\r\nConnection: close\r\n\r\n");
reply = conn.read(999999);
var result = reply.substr(reply.indexOf("\n\n"));
var showMe = JSON.parse(result)
conn.close();
}
catch(err){
fulcrum = new Window('dialog', undefined, undefined, {closeButton:true})
main = fulcrum.add("group")
main.orientation = "column"
main.alignChildren = "right"
message = main.add("group")
message.orientation = "row"
buttons = main.add("group")
buttons.orientation = "row"
buttons.alignChildren = "right"
message.add("image", undefined, File('file:///Users/constantincerdan/Desktop/skeleton-computer.png'))
message.add("statictext", [0,0,180,50], "\nFulcrum is not responding..", {multiline:true})
var tryAgain = buttons.add ("button", [0,0,70,20], "Retry", {name: "ok"});
var cancel = buttons.add ("button", [0,0,70,20], "Cancel", {name: "cancel"});
tryAgain.onClick = function() { fulcrum.close(); connectOuDlg() }
cancel.onClick = function() {fulcrum.close()}
fulcrum.show()
}
This works fine, only problem is when the user chooses to 'Retry' on the error dialog. Even though I've put fulcrum.close() in the onClick for Try Again (which just calls the main function again), the dialog stays open and another one is just spawned on top of it. So if the users decides to retry 5 times, he/she will then also have to cancel 5 times to get rid of those dialogs.
Is there a way I can stop this from happening? Any help is greatly appreciated!
Copy link to clipboard
Copied
"Should work"
Try putting the opening on a short timer so the closedown gets to finalize like "setTimeout(connectOuDlg, 200)".
Copy link to clipboard
Copied
Hi Matias, thanks for your reply! I'm still quite new to javascript - where would I add something like this into my script? I've looked for setTimeout function within the Photoshop JavasScript Reference guide and I can't find it.