Javascript变量值自动更改为NaN,我的脚本失败

Javascript变量值自动更改为NaN,我的脚本失败,javascript,jquery,ajax,Javascript,Jquery,Ajax,在我的项目中,我有以下要求:页面加载后,我必须对web服务进行异步调用(使用jquery),这大约需要1到5秒的执行时间。在同一页面中有一个“下一步”按钮,当用户在该调用完成之前单击“下一步”按钮时,页面导航应保持在保留状态。顺便说一句,我的项目在IE8版本上工作 </p> <asp:Button ID="nextButton" runat="server" Text="Next" OnClick="nextButton_Click" OnCli

在我的项目中,我有以下要求:页面加载后,我必须对web服务进行异步调用(使用jquery),这大约需要1到5秒的执行时间。在同一页面中有一个“下一步”按钮,当用户在该调用完成之前单击“下一步”按钮时,页面导航应保持在保留状态。顺便说一句,我的项目在IE8版本上工作

        </p>
        <asp:Button ID="nextButton" runat="server" Text="Next" OnClick="nextButton_Click" OnClientClick="return ValidateNextButtonClick()" />
        <br />
        some third party calls happen here
        <script type="text/javascript">
            var thirdPartyCallLoadedTime = new Date().valueOf();
        </script>

        <asp:HiddenField ID="hiddenMakeAsyncCall" runat="server" Value="true" />
    </div>
</form>
<script type="text/javascript">
    CallAsyncMethod();
</script>
同时,还有另一个第三方呼叫发生在同一页面中。这与上述ajax调用并行触发,等待时间约为2秒

        </p>
        <asp:Button ID="nextButton" runat="server" Text="Next" OnClick="nextButton_Click" OnClientClick="return ValidateNextButtonClick()" />
        <br />
        some third party calls happen here
        <script type="text/javascript">
            var thirdPartyCallLoadedTime = new Date().valueOf();
        </script>

        <asp:HiddenField ID="hiddenMakeAsyncCall" runat="server" Value="true" />
    </div>
</form>
<script type="text/javascript">
    CallAsyncMethod();
</script>
因此,我以这样的方式编写了逻辑:当用户单击next按钮时,它将首先验证2秒的等待时间,然后等待5秒,等待ajax调用完成。如果ajax调用在不到5秒钟(例如1或2秒)内完成,则跳过剩余的等待时间,并将用户导航到下一页

        </p>
        <asp:Button ID="nextButton" runat="server" Text="Next" OnClick="nextButton_Click" OnClientClick="return ValidateNextButtonClick()" />
        <br />
        some third party calls happen here
        <script type="text/javascript">
            var thirdPartyCallLoadedTime = new Date().valueOf();
        </script>

        <asp:HiddenField ID="hiddenMakeAsyncCall" runat="server" Value="true" />
    </div>
</form>
<script type="text/javascript">
    CallAsyncMethod();
</script>
下面是我的代码

<script type="text/javascript">

    var callStatus = "Pending";       // Initially ajax call status will be pending
    var holdNavigation = true;                      // Initially this will be true
    var debug = false;

    // Set debug value only if it is set int web.config
    <% #if DEBUG %>
    debug = true;
    <% #endif %>

    function CallAsyncMethod() {
        // Only make the call if the flag is set in page load
        if ($("#<%= hiddenMakeAsyncCall.ClientID %>").val() == "true") {
            Log("async call started.");
            $.ajax({
                type: "GET",
                url: "WebService.asmx/DoSomething",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: true,
                success: function (response) {
                    Log("async call completed with status - " + response.d.Status);
                    callStatus = response.d.Status;
                    holdNavigation = false;
                },
                error: function (xhr, textStatus, errorThrown) {
                    Log("async call failed.")
                    callStatus = "Error";
                    holdNavigation = false;
                    Log(errorThrown);
                }
            });
        }
        else {
            holdNavigation = false;
            callStatus = "NotRequired";
        }
    }

    var delayInterval = 500;
    var timeElapsed = 0;
    var simulateNextButtonClick = false;

    // Next button validation function
    function ValidateNextButtonClick() {
        if (!IsThirdPartyCallWaitingTimeCompleted() || (holdNavigation && !IsAsyncCallCompleted())) {
            if (timeElapsed == delayInterval * 10) {
                holdNavigation = false;
                Log("async call timed out.");
            }
            else {
                Log("Time elapsed after next button click is " + timeElapsed + " milliseconds.");
                timeElapsed += delayInterval;
                setTimeout(ValidateNextButtonClick, delayInterval);
                simulateNextButtonClick = true;
                return false;
            }
        }

        if (simulateNextButtonClick) {
            simulateNextButtonClick = false;
            Log("Simulating the next button click as it was cancelled previously.");
            $("#<%= nextButton.ClientID %>").click();
        }

        Log("Navigating to next page.");
        return true;
    }

    // Waiting time for third party call to complete is 2 seconds,
    // check whether 2 seconds is completed or not.
    function IsThirdPartyCallWaitingTimeCompleted() {
        var afterThirdPartyCallLoadedTime = (new Date().valueOf() - thirdPartyCallLoadedTime) / 1000;

        if (afterThirdPartyCallLoadedTime >= 2) {
            Log("Third party calls completed.");
            return true;
        }

        Log("Waiting for third party calls to complete.");
        return false;
    }

    // Check whether the async call is pending or not
    function IsAsyncCallCompleted() {
        if (callStatus == "Pending") {
            Log("Waiting for async call to complete.");
            return false;
        }
        else if (callStatus == "NotRequired") {
            Log("async call is not required.");
            return true;
        }

        Log("async call completed.");
        return true;
    }

    // Logs the message into console if the debug flag is set to true
    function Log(message) {
        if (debug) {
            $('#log').append(message + "<br/>");
            //window.console.log(message);
        }
    }
</script>
        </p>
        <asp:Button ID="nextButton" runat="server" Text="Next" OnClick="nextButton_Click" OnClientClick="return ValidateNextButtonClick()" />
        <br />
        some third party calls happen here
        <script type="text/javascript">
            var thirdPartyCallLoadedTime = new Date().valueOf();
        </script>

        <asp:HiddenField ID="hiddenMakeAsyncCall" runat="server" Value="true" />
    </div>
</form>
<script type="text/javascript">
    CallAsyncMethod();
</script>

var callStatus=“挂起”;//最初,ajax调用状态将处于挂起状态
var holdNavigation=true;//一开始这是真的
var-debug=false;
//仅当设置为int web.config时才设置调试值
debug=true;
函数CallAsyncMethod(){
//只有在页面加载中设置了该标志时才进行调用
if($(“#”).val()=“true”){
日志(“异步调用已启动”);
$.ajax({
键入:“获取”,
url:“WebService.asmx/DoSomething”,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
async:true,
成功:功能(响应){
日志(“异步调用已完成,状态为-”+response.d.status);
callStatus=响应d.Status;
holdNavigation=false;
},
错误:函数(xhr、textStatus、errorshown){
日志(“异步调用失败”)
callStatus=“Error”;
holdNavigation=false;
日志(错误抛出);
}
});
}
否则{
holdNavigation=false;
callStatus=“NotRequired”;
}
}
var延迟间隔=500;
var timeappeased=0;
var simulateNextButtonClick=false;
//下一步按钮验证功能
函数validateNext按钮单击(){
如果(!IsThirdPartyCallWaitingTimeCompleted()| |(holdNavigation&!IsAsyncCallCompleted()){
如果(时间经过==延迟间隔*10){
holdNavigation=false;
日志(“异步调用超时”);
}
否则{
日志(“单击下一步按钮后经过的时间为“+时间经过+”毫秒”);
时间经过+=延迟间隔;
设置超时(ValidateNext按钮单击delayInterval);
simulateNextButtonClick=true;
返回false;
}
}
如果(模拟ExtButton单击){
simulateNextButtonClick=false;
日志(“模拟单击下一步按钮,因为它以前已被取消。”);
$(“#”)点击();
}
日志(“导航到下一页”);
返回true;
}
//第三方呼叫完成的等待时间为2秒,
//检查2秒是否完成。
函数为thirdPartyCallWaitingTimeCompleted(){
第三方调用加载时间后的var=(新日期().valueOf()-thirdPartyCallLoadedTime)/1000;
如果(第三方调用加载时间>=2后){
日志(“第三方呼叫完成”);
返回true;
}
日志(“等待第三方呼叫完成”);
返回false;
}
//检查异步调用是否挂起
函数IsAsyncCallCompleted(){
如果(callStatus==“待定”){
日志(“等待异步调用完成”);
返回false;
}
else if(callStatus==“NotRequired”){
日志(“不需要异步调用”);
返回true;
}
日志(“异步调用完成”);
返回true;
}
//如果调试标志设置为true,则将消息记录到控制台中
功能日志(消息){
如果(调试){
$('#log')。追加(message+“
”); //window.console.log(消息); } }
代码

        </p>
        <asp:Button ID="nextButton" runat="server" Text="Next" OnClick="nextButton_Click" OnClientClick="return ValidateNextButtonClick()" />
        <br />
        some third party calls happen here
        <script type="text/javascript">
            var thirdPartyCallLoadedTime = new Date().valueOf();
        </script>

        <asp:HiddenField ID="hiddenMakeAsyncCall" runat="server" Value="true" />
    </div>
</form>
<script type="text/javascript">
    CallAsyncMethod();
</script>

        </p>
        <asp:Button ID="nextButton" runat="server" Text="Next" OnClick="nextButton_Click" OnClientClick="return ValidateNextButtonClick()" />
        <br />
        some third party calls happen here
        <script type="text/javascript">
            var thirdPartyCallLoadedTime = new Date().valueOf();
        </script>

        <asp:HiddenField ID="hiddenMakeAsyncCall" runat="server" Value="true" />
    </div>
</form>
<script type="text/javascript">
    CallAsyncMethod();
</script>

这里有一些第三方电话 var thirdPartyCallLoadedTime=新日期().valueOf(); CallAsyncMethod();
当我在一个新的应用程序中测试时,这段代码是有效的,但当我在我的项目中尝试时,它不起作用。只有在IE中打开开发者工具窗口时,它才起作用。调试时,我发现interval和TimeAppead的变量自动更改为NaN

        </p>
        <asp:Button ID="nextButton" runat="server" Text="Next" OnClick="nextButton_Click" OnClientClick="return ValidateNextButtonClick()" />
        <br />
        some third party calls happen here
        <script type="text/javascript">
            var thirdPartyCallLoadedTime = new Date().valueOf();
        </script>

        <asp:HiddenField ID="hiddenMakeAsyncCall" runat="server" Value="true" />
    </div>
</form>
<script type="text/javascript">
    CallAsyncMethod();
</script>

请帮我解决这个问题。另外,请告诉我是否有更好的方法来实现我所做的操作。

但我不会将任何值除以零,所有值都在页面中,并且是脚本中发生的唯一乘法。请尝试重命名变量。它们是全局的(存储在
窗口.delayInterval
窗口.timeappeased
)并且可能与其他代码(可能是第三方代码)冲突。尝试使用诸如
Harsha\u delayInterval
之类的名称使其唯一。在数字上下文中使用时,未定义的值将强制为
NaN
。很可能您的一些变量未定义(未初始化),这是一个很好的观点,但我确实尝试过,我在这些变量前面加上了我的页面名称前缀,但仍然存在相同的问题。难道没有其他方法检测第三方完成呼叫吗?只等2秒钟,希望它能结束是愚蠢的。你可以简单地调用一个方法,而不是等待5秒钟
        </p>
        <asp:Button ID="nextButton" runat="server" Text="Next" OnClick="nextButton_Click" OnClientClick="return ValidateNextButtonClick()" />
        <br />
        some third party calls happen here
        <script type="text/javascript">
            var thirdPartyCallLoadedTime = new Date().valueOf();
        </script>

        <asp:HiddenField ID="hiddenMakeAsyncCall" runat="server" Value="true" />
    </div>
</form>
<script type="text/javascript">
    CallAsyncMethod();
</script>