Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在不等待setInterval获取结果的情况下获取setInterval工作?_Javascript_C# - Fatal编程技术网

Javascript 如何在不等待setInterval获取结果的情况下获取setInterval工作?

Javascript 如何在不等待setInterval获取结果的情况下获取setInterval工作?,javascript,c#,Javascript,C#,我在ASP.NET应用程序中使用javascript的$.get()函数从另一个页面获取一些数据,并将接收到的数据设置为标签的文本之一 我还将其设置为setInterval()函数,以持续检查新数据。但是问题是:每次我加载一个新页面时,脚本都会调用$.get()函数,并且3秒钟内没有任何内容,从客户端的角度来看,我发现这并不令人满意 是否有某种方法可以让我在手之前将值设置到标签中,然后在后台自动刷新以检查新值 以下是我写的脚本: <script type="text/javascript"

我在ASP.NET应用程序中使用javascript的
$.get()
函数从另一个页面获取一些数据,并将接收到的数据设置为标签的文本之一

我还将其设置为
setInterval()
函数,以持续检查新数据。但是问题是:每次我加载一个新页面时,脚本都会调用
$.get()
函数,并且
3秒钟内没有任何内容,从客户端的角度来看,我发现这并不令人满意

是否有某种方法可以让我在手之前将值设置到标签中,然后在后台自动刷新以检查新值

以下是我写的脚本:

<script type="text/javascript">

        function myFunction() {

            $.get("AjaxServicesNoty.aspx", function (data) {

                var recievedCount = data;
                var existingCount = $("lblEventCount").text();


                if (existingCount == "") {
                    $(".lblEventCount").html(recievedCount);
                    $(".lblAcceptedCount").html(recievedCount);

                }

                else if (parseInt(recievedCount) > parseInt(existingCount)) {
                    $(".lblEventCount").html(recievedCount);
                    $(".lblAcceptedCount").html(recievedCount);

                }
                else {
                    $(".lblEventCount").html(existingCount);
                    $(".lblAcceptedCount").html(existingCount);
                }

            });

                }

            setInterval(myFunction,3000);
        </script>

函数myFunction(){
$.get(“AjaxServicesNoty.aspx”,函数(数据){
var ReceivedCount=数据;
var existingCount=$(“lblEventCount”).text();
如果(existingCount==“”){
$(“.lblEventCount”).html(ReceivedCount);
$(“.lblAcceptedCount”).html(ReceivedCount);
}
else if(parseInt(receivedcount)>parseInt(existingCount)){
$(“.lblEventCount”).html(ReceivedCount);
$(“.lblAcceptedCount”).html(ReceivedCount);
}
否则{
$(“.lblEventCount”).html(现有计数);
$(“.lblAcceptedCount”).html(现有计数);
}
});
}
设置间隔(myFunction,3000);

如果希望在页面加载后执行函数,只需如下调用:

$( document ).ready(function() {
    myFunction();
});

它将只执行一次,但请注意,如果它们异步执行(默认情况下),它可能与setInterval调用重叠。

加载页面时只需调用myFunction()。函数开始之前?我应该调用它吗?只需在脚本的第一行调用函数作为
myFunction()
,而不是在
$(文档)中调用它。ready()
会导致任何差异吗?是的,因为在得到ajax响应后更新页面(DOM),需要加载页面。理想情况下,页面加载将比ajax调用更快,但为了安全起见,请在documentready.Okkay中调用函数。。谢谢你,黑蓝精灵@总成:谢谢你的澄清@总成:我注意到,直接在脚本的第一行调用函数比在
$(document).ready()中调用更快。这种延迟是因为DOM生成所花费的时间吗?