English 中文(简体)
在递回循环中的运行函数之间等待
原标题:Wait between running functions in recursive loop

我创建了一个循环循环以删除 SP Services 的 SharePoint 列表中的项目。 我放了一个5 秒的超时; 但出于某种原因, 它只一次删除 2 个, 然后执行等待而不是删除批次, 然后重复此函数 。

请参阅我的代码如下:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/0.7.1a/jquery.SPServices-0.7.1a.min.js"></script>
<script type="text/javascript">

    $(document).ready(function () {
        $intStart = 1;
        $intCount = 10;
        $intEnd = 40;

        $("input[class= buttonpush ]").click(function () {
            var looper = function () {
                if ($intStart < $intCount && $intStart < $intEnd) {
                    $().SPServices.SPUpdateMultipleListItems({
                        webURL: "http://widgettest",
                        listName: "TestList",
                        CAMLQuery: "<Query><Where><And><Gt><FieldRef Name= ID  /><Value Type= Counter >" + $intStart + "</Value></Gt><Lt><FieldRef Name= ID  /><Value Type= Counter >" + $intCount + "</Value></Lt></And></Where></Query>",
                        batchCmd: "Delete",
                        valuepairs: [],
                        debug: false,
                        completefunc: function (xData, Status) {
                            $intStart = $intStart + 5;
                            $intCount = $intCount + 5
                            setTimeout(looper, 5000);
                        }
                    });
                }
                else { 
                }
            }
            looper();
        });
    });

</script>
最佳回答

OK, 既然您显示初始呼叫 loooper () () >, 逻辑似乎没有问题。 loooper () 应该运行8次, 直到 $intStart 等于41 。 这些是您可能得不到您所期望的连续循环器运行次数( ) 多少的原因 :

  1. You get an error from $().SPServices.SPUpdateMultipleListItems() and completeFunc is not executed like you are expecting.
  2. Something else in your code is manipulating the values of the global variables $intStart, $intCount or $intEnd so they get changed accidentally while you are running and thus the loop doesn t run as many iterations as expected.
  3. You get a javascript exception somewhere during the running of looper() that causes execution to stop.

要调试此选项, 请在调用 concolole. log () 语句前先插入一个独有的 concole. log () 语句。 SPSPervitss. SP Update MultiList Litters () 和另一个在 < code > completefulFunc 中的语句, 以便您在停止前能准确知道它取得多少进展 。

关于该守则的其他意见如下:

  1. $intStart is always less than $intCount since it starts less and both are incremented by 5 so you don t need that part of the if statement.
  2. It is not customary to preceded javascript variables with a $ sign unless you are connoting some special kind of variable. That is not the case here and just impedes the readability of the javascript to someone familiar with javascript.
  3. Your variables $intStart, $intCount or $intEnd should probably not be global variables and in all cases should not be implicitly declared or accidentally declared globals. If you mean for them to be globals, then declare them as globals in the global scope using var. If you mean for them to be local to some scope, then declare them in that scope.
  4. Because your variables are global and are only initialized one at document.ready() time, your button push will only work the first time. It will never do anything when pushed a second time.
  5. Your code is not protected from the user pushing the button again while looper() is running (this will cause duplicate requests to be launched).

您的 CAML 查询将会是前两次 :

<Query>
    <Where>
        <And>
            <Gt>
                <FieldRef Name= ID  />
                <Value Type= Counter >1</Value>
            </Gt>
            <Lt>
                <FieldRef Name= ID  />
                <Value Type= Counter >10</Value>
            </Lt>
        </And>
    </Where>
</Query>"    

<Query>
    <Where>
        <And>
            <Gt>
                <FieldRef Name= ID  />
                <Value Type= Counter >6</Value>
            </Gt>
            <Lt>
                <FieldRef Name= ID  />
                <Value Type= Counter >15</Value>
            </Lt>
        </And>
    </Where>
</Query>"    

这在我看来不正确。 第一次, 您要求处理 & gt; 1 & amp; & amp; & lt; 10. 第二次, 您要求处理 & gt; 6 & amp; & pl; 15 。

第二次不应该是 & gt; 9 & amp; & amp; & lt; 18 来获得尚未处理的新文件吗?

问题回答

暂无回答




相关问题
SharePoint - Approaching Website Storage Limit Email

How can i go about changing the distribution list as well as the email text for the email that goes out to site collection admin when a site collection approaches it s size limit? Thanks for your ...

UI automated testing within SharePoint

I m looking for automated Functional Testing tools that can manipulate SharePoint sites, libraries, and documents thought the web interface. It needs to be extensible enough to handle any custom ...

Enable authorization on sitemap provider

I want to enable Authorization on the Site map provider. We have enabled anonymous access to the site and we want the Site map provider to be visible only to authorized users. I tried ...

SharePoint : web service permission error

I have a sharepoint site, and I am calling a standard sharepoint web service. I create the web service request like this : wsDws.Url = this.SiteAddress + @"/_vti_bin/Dws.asmx"; When I use ...

Sharepoint 2007 Data view Webpart custom parameters

I m sort of new to the custom parameters that can be setup on a DataView Webpart. There are 6 options: - None - Control - Cookie - Form - QueryString - Server Variable I think that None, Cookie and ...