setinterval

How to change the interval dynamically when using setInterval

爱⌒轻易说出口 提交于 2020-01-14 10:16:11
问题 I have this fiddle : https://jsfiddle.net/reko91/stfnzoo4/ Im currently using Javascripts setInterval() to log a string to console. What I want to do, is in this setInterval function check whether the interval variable has changed, if it has, change the interval in the setInterval function. I can lower the interval variable by 100 (speeding the function up) by a click a button. Is this possible ? Someone mentioned this : Changing the interval of SetInterval while it's running But this is

How to change the interval dynamically when using setInterval

白昼怎懂夜的黑 提交于 2020-01-14 10:16:07
问题 I have this fiddle : https://jsfiddle.net/reko91/stfnzoo4/ Im currently using Javascripts setInterval() to log a string to console. What I want to do, is in this setInterval function check whether the interval variable has changed, if it has, change the interval in the setInterval function. I can lower the interval variable by 100 (speeding the function up) by a click a button. Is this possible ? Someone mentioned this : Changing the interval of SetInterval while it's running But this is

jQuery load something based on whose class is “active”

此生再无相见时 提交于 2020-01-14 05:17:05
问题 I'm trying to use this with tabs that load content with ajax into a div. I can't get it to refresh on the interval. The top part does work, however. <script type="text/javascript"> $(function rlAl() { if ($("#xicon1").hasClass("active")) { $("#actionlist").load("alcurrent.php"); } else if ($("#xicon2").hasClass("active")) { alert("icon2"); } else if ($("#xicon3").hasClass("active")) { alert("icon3"); } }); $(function() { setInterval(rlAl, 5000); }); </script> 回答1: rlAl is undefined because it

Javascript setInterval runs only one time

徘徊边缘 提交于 2020-01-14 03:48:05
问题 setInterval(this.Animate(), this.speed); This is expected to be run every this.speed times. Yes, but the browsers run it only one time. What are the possible reasons for that? 回答1: If Animate is a derived function from the prototype, you'll have to use: setInterval(this.Animate.bind(this), this.speed); 回答2: Try to run your function without the parentheses, when you put parentheses it always calls the function instead of passing it, which is what you want here: setInterval(this.Animate, this

JavaScript - setInterval only running once

佐手、 提交于 2020-01-14 03:03:48
问题 http://jsfiddle.net/689nauny/ setInterval() is only running once... WTF is going on? SO is asking for more details but providing a JSFiddle is about as descriptive as I can be? I've tried using an anonymous function and now a callback. I just don't get it? :-/ HTML <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script> <div id="qanda-timer-container"> <div class="qanda-timer"> <span id="qanda-time-remaining"></span> </div> </div> JS function intervalFunc(thinkingTime, answerTime)

Js动画(一)基础

為{幸葍}努か 提交于 2020-01-13 13:29:56
  在 再谈js拖拽(二)仿iGoogle自定义首页模块拖拽 的最后,我说了接下来要写Js动画,转瞬拖到了今天,呵呵。这篇主要讲动画的基础,就是几个最基本的特效,即:移动,渐变和尺寸变化。接下来写个梦幻西游版逍遥生角色行走的动画,然后再适时的写些动画有关的例子,争取把这个系列写好。   我们玩魔兽世界的时候可以通过ctrl+r来查看当前的帧数,当帧数很小时,会觉得很卡,帧数很高则很流畅。所谓帧数就是1秒内显示图片的数量。当这么多帧图片连起来显示,就形成了动画。   Js中实现动画都是靠setInterval或者setTimeout来实现。setInterval自身就能不断循环来执行计算从而显示新的帧,setTimeout是间隔一段时间后仅执行一次,他需要配合函数循环实现,很多人偏爱setTimeout来实现。本文采用setInterval实现。   据说,普通人眼能看到1/24秒,就是说1秒至少24帧,每次移位间隔需要小于1000/24=41.7毫秒,也就说setInterval要每隔至少40毫秒执行一次,一般地,我们采用10毫秒,当然间隔时间越短,客户端执行计算次数就越多,如果你code计算量大则可以适当调长些。 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR

js休眠实现sleep[博]

会有一股神秘感。 提交于 2020-01-12 13:51:47
首先js原生并不支持sleep,所有目前的方案都是间接实现的. 需求: for (i=0;i<100;i++){ echartsPlots(xxx[i]); } 其中echartsPlots里面是ajax调用后台,直接执行后台极其容易卡死,里面的数据查询和处理都较慢.所以希望echartsPlots执行后,sleep一段时间,然后继续循环,执行下一个.任务. 方法1,循环实现 function sleep(delay) { for (var t = Date.now(); Date.now() - t <= delay;) ; } 最简单,最坑爹的方法 之所以说坑爹,主要是因为效果上,的确实现了sleep的目的,但实际以牺牲客户端效率的代价,毕竟cpu在空转. 那么,可以达到我期望效果? for (i=0;i<100;i++){ echartsPlots(xxx[i]); sleep(1000)//毫秒 } 实际是不行(ajax请求都停留在pending状态,实际后端已经返回),原因请自己想下,提示(js本质单线程) 方法2,setTimeout(同setInterval) function loop(i) { console.log(i); i++; if (i<10) setTimeout(function () { loop(i); }, 2000); } loop(0);

0180 定时器 之 setInterval() :开启定时器,京东倒计时案例,停止定时器,发送短信倒计时案例

末鹿安然 提交于 2020-01-11 18:38:15
1、开启定时器 timeout:暂停; 超时。 interval: (时间上的) 间隔,间隙,间歇。 <script> // 1. setInterval setInterval(function() { console.log('继续输出'); }, 1000); </script> 2、案例:京东倒计时 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> div { margin: 200px; } span { display: inline-block; width: 40px; height: 40px; background-color: #333; font-size: 20px; color: #fff; text-align: center; line-height: 40px; } </style> </head> <body> <div> <span class="hour">1

How to use setInterval or setTimeout with a for loop?

心已入冬 提交于 2020-01-11 10:26:14
问题 I am trying to set an interval when some code runs but only need to do it based on the number of elements there are. Here's a quick example: 5 total elements Run code once every 20 seconds for each element that is found. Can someone please give me a basic example how to do this using plain JavaScript? Everything I have tried just executes all of the code at once instead of doing one element at a time. 回答1: let's suppose you're talking about elements of an array or a DOM collection (function()

Listeners in Chrome dev tools' performance profiling results

北城余情 提交于 2020-01-11 08:25:33
问题 I have been profiling a React app using Chrome dev tools and I've discovered a linearly increasing Listener count. Take a look at the screenshot below. The Listeners are in orange. I narrowed it down to a simple countdown value render inside p tags. The remaining time is generated using setInterval function every 1000ms and then formatted and rendered inside the p tags. I created a simple React app using create-react-app and modified the code inside the App.js's App component to update the