瀏覽代碼

add osmd.loadUrlTimeout (default 9s) for load(url)/ajax(), fix test error/timeout (hopefully)

sschmid 4 年之前
父節點
當前提交
8ced341d2e
共有 2 個文件被更改,包括 5 次插入2 次删除
  1. 2 1
      src/OpenSheetMusicDisplay/AJAX.ts
  2. 3 1
      src/OpenSheetMusicDisplay/OpenSheetMusicDisplay.ts

+ 2 - 1
src/OpenSheetMusicDisplay/AJAX.ts

@@ -7,8 +7,9 @@ export class AJAX {
      * @param url
      * @returns {any}
      */
-    public static ajax(url: string): Promise<string> {
+    public static ajax(url: string, timeout: number = 9000): Promise<string> {
         let xhttp: XMLHttpRequest;
+        xhttp.timeout = timeout;
         const mimeType: string = url.indexOf(".mxl") > -1 ? "text/plain; charset=x-user-defined" : "application/xml";
         if (XMLHttpRequest) {
             xhttp = new XMLHttpRequest();

+ 3 - 1
src/OpenSheetMusicDisplay/OpenSheetMusicDisplay.ts

@@ -64,6 +64,8 @@ export class OpenSheetMusicDisplay {
     public cursor: Cursor;
     public zoom: number = 1.0;
     private zoomUpdated: boolean = false;
+    /** Timeout in milliseconds used in osmd.load(string) when string is a URL. */
+    public loadUrlTimeout: number = 9000;
 
     private container: HTMLElement;
     private backendType: BackendType;
@@ -124,7 +126,7 @@ export class OpenSheetMusicDisplay {
                 log.debug("[OSMD] Retrieve the file at the given URL: " + trimmedStr);
                 // Assume now "str" is a URL
                 // Retrieve the file at the given URL
-                return AJAX.ajax(trimmedStr).then(
+                return AJAX.ajax(trimmedStr, this.loadUrlTimeout).then(
                     (s: string) => { return self.load(s); },
                     (exc: Error) => { throw exc; }
                 );