标签: setinterval

setInterval的这种行为是否意味着Javascript中的多线程行为?

在使用JavaScript我注意到这件事.您可以使用

var i=0; 
var startingTime=new Date().getTime();
setInterval("foo()",1);
function foo() {
    i+=1;
    if ($("#foodiv").text()==i) {
        //we detected a doubled value (parallel execution)
        $("#repdiv").append("[repetition on "+i+"]");
    }
    $("#foodiv").html(i);
    $("#timediv").html(Math.floor((new Date().getTime()-startingTime)/1000));
}
Run Code Online (Sandbox Code Playgroud)

但是当我阅读并尝试自己时,时间不是1毫秒,它至少是10毫秒或者其他东西.实际上10秒后,我的值大约为2300/2400,而不是预期的10000.

这是程序的最小可能时间因素??? 肯定没有.如果我试试这个:

<html><head>
<script language="javascript" type="text/javascript" src="jquery-1.4.min.js"></script>
<script type="text/javascript">

var i=0;
var startingTime=new Date().getTime();

setInterval("foo()",1);setInterval("foo()",1);setInterval("foo()",1);
setInterval("foo()",1);setInterval("foo()",1);setInterval("foo()",1);
setInterval("foo()",1);setInterval("foo()",1);setInterval("foo()",1);
setInterval("foo()",1);setInterval("foo()",1);setInterval("foo()",1);
setInterval("foo()",1);setInterval("foo()",1);setInterval("foo()",1);
setInterval("foo()",1);setInterval("foo()",1);setInterval("foo()",1);
setInterval("foo()",1);setInterval("foo()",1);setInterval("foo()",1);
setInterval("foo()",1);setInterval("foo()",1);setInterval("foo()",1);

function foo() {
    i+=1;
    if ($("#foodiv").text()==i) {
        //we detected a doubled value (parallel execution)
        $("#repdiv").append("[repetition on "+i+"]");
    }
    $("#foodiv").html(i);
    $("#timediv").html(Math.floor((new Date().getTime()-startingTime)/1000));

}
</script>
</head>
<body>
<div id="foodiv"></div>  (counter) …
Run Code Online (Sandbox Code Playgroud)

javascript multithreading setinterval

5
推荐指数
2
解决办法
4156
查看次数

jquery禁用表单元素

场景是我在表单元素上编写了一个自动刷新功能,我想在登录按钮上点击onclick事件来禁用自动刷新功能.

我无法禁用autorefresh,页面仍然刷新.

我的代码是:

$('#form').ready(function () { //Increment the idle time counter every minute.
    var idleInterval = setInterval("timerIncrement()", 15000); // 1 minute

    //Zero the idle timer on mouse movement.
    $(this).mousemove(function (e) {
        idleTime = 0;
    });
    $(this).keypress(function (e){
        idleTime = 0;
    });
});

function timerIncrement() {
    idleTime = idleTime + 1;
    if (idleTime > 2) { // 20 minutes
        window.location.reload();
    }
}

$('#login').ready(function () {

    alert("working promptly");
    //Zero the idle timer on mouse movement.
    $(this).click(function (e) {
        alert("Hi In click !"); …
Run Code Online (Sandbox Code Playgroud)

javascript jquery setinterval clearinterval

5
推荐指数
1
解决办法
263
查看次数

使用Javascript在网页上显示另一台计算机的时间?

我正在开发一个非常时间敏感的Web应用程序.给我的业务规则之一是应用程序的行为必须始终取决于Web服务器上的时间,无论客户端的时钟是什么时候.为了向用户说明这一点,我被要求在Web应用程序中显示服务器的时间.

为此,我写了以下Javascript代码:

clock = (function () {
var hours, minutes, seconds;

function setupClock(updateDisplayCallback) {
    getTimeAsync(getTimeCallback);

    function getTimeCallback(p_hours, p_minutes, p_seconds) {
        hours = p_hours;
        minutes = p_minutes;
        seconds = p_seconds;
        setInterval(incrementSecondsAndDisplay, 1000);
    }

    function incrementSecondsAndDisplay() {
        seconds++;
        if (seconds === 60) {
            seconds = 0;
            minutes++;
            if (minutes === 60) {
                minutes = 0;
                hours++;
                if (hours === 24) {
                    hours = 0;
                }
            }
        }
        updateDisplayCallback(hours, minutes, seconds);
    }
}

// a function that makes an AJAX call and invokes …
Run Code Online (Sandbox Code Playgroud)

javascript time jquery setinterval

5
推荐指数
2
解决办法
583
查看次数

多次运行setInterval,然后暂停几秒钟

我需要我的代码运行x次,然后暂停30秒左右再恢复.有任何想法吗?

myslidefunction();
var tid = setInterval(myslidefunction, 1000);

function myslidefunction() {
    setTimeout(function () {
        //do stuff  
    }, 400); 
};
Run Code Online (Sandbox Code Playgroud)

javascript jquery settimeout setinterval

5
推荐指数
1
解决办法
238
查看次数

为什么setInterval()循环每次都变得更快?

我正在Javascript上构建一个自定义滑块,我希望每次用户点击滑块的div时,滑块应停止X秒.

我的代码是:

$(document).ready(function () {
    var ciclo;
    var index_slide = 1;

    function startSlidercicle() {
        ciclo = setInterval( function() {
            // Slider code goes here
        }, 3000); 
    }

    //Here I start the slider animation
    startSlidercicle();

    //When the user clicks on a div called 'slide', stop the cycle and start again the animation cycle
    $('.slide').on('click', function() {
        clearInterval(ciclo);
        setTimeout(startSlidercicle(), 3000);
    });
});
Run Code Online (Sandbox Code Playgroud)

但问题是每次我点击并停止滑块时,循环开始越来越快.我该如何解决?

javascript jquery settimeout setinterval clearinterval

5
推荐指数
1
解决办法
5027
查看次数

为什么setInterval()会忽略错误?

Javascript的setInterval()似乎不关注它调用的代码是否抛出异常.例如,这不会终止程序,而是一遍又一遍地调用函数:

setInterval(function() { throw "error" }, 1000);
Run Code Online (Sandbox Code Playgroud)

这种行为的原因是什么?是否记录在任何地方?

javascript exception function setinterval

5
推荐指数
1
解决办法
1377
查看次数

使用setInterval时如何动态更改间隔

我有这个小提琴:https://jsfiddle.net/reko91/stfnzoo4/

我目前正在使用Javascript setInterval()记录字符串以进行控制台。

我想做的是在此setInterval函数中检查interval变量是否已更改,如果已更改,请更改setInterval函数中的间隔。我可以通过单击按钮将间隔变量降低100(加快功能)。

这可能吗 ?

有人提到了这一点:在运行SetInterval时更改其间隔

但这使用了一个计数器,因此他们只运行一定的次数。我需要运行很长时间,但是要更改再次调用该函数的速度。

这是代码:

var interval = 2000;

setInterval(function() {
  interval = getInterval();
  console.log('interval')
}, interval);


function getInterval() {
  return interval;
}


$('#speedUp').on('click', function() {
  interval -= 100;
  console.log(interval)
})
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id='speedUp'>
  speed up
</button>
Run Code Online (Sandbox Code Playgroud)

javascript jquery setinterval

5
推荐指数
1
解决办法
5728
查看次数

Javascript setInterval每5秒

我想在每个警报之间设置5秒的间隔.找到这个帖子:

setInterval(function() {
    alert("Message to alert every 5 seconds");
}, 5000);
Run Code Online (Sandbox Code Playgroud)

但是我setInterval()每隔5秒钟才能发出警报?

$(window).scroll(function() {
    if (checkVisible($('#footer'))) {
        alert("I DONT SEE A FOOTER");
    } else {
        alert("EUREKA - I SEE A FOOTER");
    }
});

function checkVisible(elm, eval) {
    eval = eval || "visible";
    var vpH = $(window).height(), // Viewport Height
        st = $(window).scrollTop(), // Scroll Top
        y = $(elm).offset().top,
        elementHeight = $(elm).height();

    if (eval == "visible") 
        return ((y < (vpH + st)) && (y > (st - …
Run Code Online (Sandbox Code Playgroud)

javascript jquery setinterval

5
推荐指数
1
解决办法
9165
查看次数

clearInterval()不使用JavaScript的时钟计时器

我对JavaScript和编程很新.我目前正处于一个小腌菜中,我正在玩一些代码,我想知道是否有人可以给我一些建议.

背景:

我正在使用的代码非常简单; 有一个时钟,当前时间在setInterval上运行,以便按秒更新.

在时钟下方有一个按钮,显示"停止",按下时,它将清除间隔,然后按钮将显示"开始".如果按下再次按下"开始"的按钮,它将继续时钟计时器在当前时间.所以基本上这一个按钮切换时钟的间隔,并且根据它的状态,按钮将显示"开始"或"停止".

W3Schools:JS Timing是我在创建我正在使用的代码时最初引用的地方.这是我在学习setInterval和clearInterval如何工作的地方.我还在示例中采用了一些代码并对其进行了调整,以便我可以尝试关闭和打开时钟计时器.

码:

var clock09 = window.setInterval(myTimer09, 1000);

function myTimer09() {
  var d = new Date();
  var t = d.toLocaleTimeString();
  document.getElementById("req09").innerHTML =
    "<h1>" + t + "</h1>";
}

function toggle10() {
  var button = document.getElementById("button10").innerHTML;
  if (button == "Stop") {
    window.clearInterval(clock09);
    document.getElementById("button10").innerHTML = "Start";
  } else {
    clock09 = window.setInterval(myTimer09, 1000);

    document.getElementById("button10").innerHTML = "Stop";
  }
}
Run Code Online (Sandbox Code Playgroud)
<span class="center" id="req09"></span>
<button type="button" id="button10" onclick="toggle10()" class="button">Stop</button>
Run Code Online (Sandbox Code Playgroud)

https://jsfiddle.net/dtc84d78/

问题:

所以我的代码问题是按钮从"停止"按钮切换到"开始"按钮,但是clearInterval不使用setInterval应用于变量.

我在谷歌搜索过类似的问题,比如这个,我听从了他们的建议,但仍然没有.经过几个小时的努力弄清楚,我决定只将W3Schools的一些例子直接复制并粘贴到jsFiddle,这甚至都不起作用(包含在jsfiddle链接中)?

我真的只是疯了为什么clearInterval() …

html javascript setinterval jsfiddle clearinterval

5
推荐指数
1
解决办法
630
查看次数

JavaScript是否用完了超时ID?

令人惊讶的是,我在网络上的任何地方都找不到该问题的答案。

在文档中声明 setTimeout和setInterval共享相同的ID池,并且ID 永远不会重复。如果是这种情况,那么它们最终必须耗尽,因为计算机可以处理的最大数量是多少?然后会发生什么,您不能再使用超时了?

javascript settimeout setinterval

5
推荐指数
1
解决办法
259
查看次数