简体   繁体   中英

Issue on returning a variable from an evalScript function

I am working on a CEP HTML panel for Photoshop and I want to check first if there is any opened documents before my panel do what it has to do. So I made something like this into my index.js so to test if I get the correct result. But psDocumentsLength variable returns as undefined. Any idea what am I doing wrong?

(function()
{
   'use strict';

    var csInterface = new CSInterface();
    var psDocumentsLength; //1//

    function init()
    {
        themeManager.init();
        $(document).ready(function()
        {
            check_PSDocumentsLength();
            alert(psDocumentsLength); //4//
        });
    };

    init();

    function check_PSDocumentsLength() //2//
    {
        var chosenFunction = 'checkDocumentsLength()';
        csInterface.evalScript(chosenFunction, function(result)
        {
            psDocumentsLength = result; //3//
        });
    };

}());

Considering what Sergey Kritskiy said at graphicdesign on stackexchange about issue of code async , I tried to add a setTimeout after check_PSDocumentsLength() function call and it worked... So my code now looks like this...

    (function()
    {
       'use strict';

        var csInterface = new CSInterface();
        var psDocumentsLength; //1//

        function init()
        {
            themeManager.init();
            $(document).ready(function()
            {
                check_PSDocumentsLength();
                setTimeout(function()
                {
                    alert(psDocumentsLength);
                }, 1000); //4//
            });
        };

        init();

        function check_PSDocumentsLength() //2//
        {
            var chosenFunction = 'checkDocumentsLength()';
            csInterface.evalScript(chosenFunction, function(result)
            {
                psDocumentsLength = result; //3//
            });
        };

    }());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM