开发者

No data in callback : how to pause load of a javascript function

开发者 https://www.devze.com 2023-03-23 15:43 出处:网络
We are having an issue in the following javascript code. doCallback function is happening before doMainProcess gets finished.

We are having an issue in the following javascript code.

doCallback function is happening before doMainProcess gets finished.

So every time we get result = null in the doCallback.

Is there a way to pause load of the doCallback to wait until we get the result ?

Edit: setResult is happening multiple times and is asynchronous via iframe, and we don't know timing. Also callback only happens some of the time decided by another process.

So we can not simply change the position of doCallback.

<html>
<head>
<script>
 var result;
 var callback = "callback";
 var url = "h开发者_如何学运维ttp://www.example2.com/getResponse/";

 function iframeCallback() {
    var iframe = document.createElement('iframe');
    iframe.style.border='0px';
    iframe.style.width ='0px';
    iframe.style.height='0px';
    document.body.appendChild(iframe);
    var iDocument;
    if (iframe.contentDocument) {
        iDocument = iframe.contentDocument; 
    } else if (iframe.contentWindow) {
        iDocument = iframe.contentWindow.document;
    } else if (iframe.document) {
        iDocument = iframe.document;
    }
    var content = "<script type='text/javascript'>";
    content += "var jsText = \"<script type='text/javascript' src='" + url + "'></\" + \"script>\";";
    content += "document.write(jsText);";
    content += "</"+"script>";
    content += "<script type='text/javascript'>";
    content += "var data = eval('"+callback+"');";
    content += "window.parent.setResult(data);";
    content += "</"+"script>";
    iDocument.open();
    iDocument.write(content);
    iDocument.close();
}

function setResult(data) {
    result = data;
}
function doMainProcess() {

    iframeCallback()
}

function doCallback() {

            //we need to wait here until we get the result.

    alert(result);
}
</script>
</head>
<body>
<script>
    doMainProcess();
</script>
<script>
    doCallback();
</script>
</body>
<html>


Yes,

delete this:

<script>
   doCallback();
</script>

Change this:

function setResult(data) {
    result = data;
}

to this:

function setResult(data) {
    result = data;
    doCallback();
}


A clunky solution (that doesn't involve my having to read your code carefully):

var readyForCallback = false;

function doMainProcess() {
  // your code here

  readyForCallback = true;
}

function doCallback(arg1,arg2,arg3,etc) {
  if (!readyForCallback) {
    // anonymous function as way to keep the original callback
    // argument(s) with a timeout 
    setTimeout(function(){doCallback(arg1,arg2,arg3,etc);},20);
    return;
  }

  // your code here
}

Note: within your timeout function you could also use doCallback.apply() with the arguments object to automatically handle any number of arguments, but I didn't include this in my code because off-hand I forget whether you can just use the arguments object directly or if you'd have to first create a proper array populated from arguments.

0

精彩评论

暂无评论...
验证码 换一张
取 消