简体   繁体   English

从函数返回值之前保存的空变量

[英]empty variable getting saved before value is returned from a function

I am trying to save a data using the below query 我正在尝试使用以下查询保存数据

var content = framejson($(e.target));
        //alert(wpId);
        var dfd = $.Deferred();
        setTimeout(function(){
        saveWebPartProperties(wpId, { Description: content }).done(function () {
                dfd.resolve();
                $(e.target).closest("table").find(".successmessage").show();
            }).fail(self.error);
        },500);

The code for framejson is below 以下是framejson的代码

function framejson(elem){
var listname={};
listname["Name"] = $(elem).closest("table").find(".Name").val();
listname["Length"] = $(elem).closest("table").find(".Length").val();
return JSON.stringify(listname);
};

Now in the code where I am trying to save the json content, empty value gets saved when I remove the alert or remove the setTimeout 现在在我尝试保存json内容的代码中,删除警报或删除setTimeout时将保存空值

It works fine only if I have the alert or setTimeout or if I debug via console 仅当我有警报或setTimeout或通过控制台调试时,它才能正常工作

I also tried to use deferred and when but it didn't work. 我也尝试使用deferredwhen使用,但没有用。 Only setTimeout is making it work and I am trying to understand why 只有setTimeout使它工作,我试图理解为什么

var content = $.Deferred();
        content.resolve(framejson($(e.target)));
        $.when(content).done(function(){
            var dfd = $.Deferred();
            saveWebPartProperties(wpId, { Description: content }).done(function () {
                dfd.resolve();
                $(e.target).closest("table").find(".successmessage").show();
            }).fail(self.error);
        });

What is the mistake that is causing this issue? 导致此问题的错误是什么?

As you can see in this snippet, this works fine. 如您在此代码段中所见,这很好用。 So I guess your problem isn't in this showed code. 因此,我想您的问题不在此显示的代码中。

 $(".test").on('click', function(e){ var content = framejson(e.target); console.log(content) }); function framejson(elem){ var listname={}; listname["Name"] = $(elem).closest("table").find(".Name").val(); listname["Length"] = $(elem).closest("table").find(".Length").val(); return JSON.stringify(listname); }; 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td><input class="Name" value="bla" /></td> <td><input class="Length" value="ert" /></td> <td><button class="test">test</button></td> </tr> </table> 

Below code worked, 下面的代码有效,

var wpId=$(e.target).parents().closest(".ms-rtestate-field").parent().attr("webpartid");
        var content = $.Deferred();
        content.resolve(framejson($(e.target)));
        $.when(content).done(function(value){
            var dfd = $.Deferred();
            saveWebPartProperties(wpId, { Description: value }).done(function () {
                dfd.resolve();
                $(e.target).closest("table").find(".successmessage").show();
            }).fail(self.error);
        });

I think the reason is, before the content is returned value from the framejson function, the save part is getting called 我认为原因是,在从framejson函数返回内容的值之前,调用了保存部分

I have used deferred and when to resolve this problem. 我曾使用deferredwhen解决此问题。 Once the content gets value returned from the function, the rest of the code is getting called for saving it 一旦内容从函数返回值,就会调用其余代码来保存它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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