settimeout

深入理解JS函数中this指针的指向

爱⌒轻易说出口 提交于 2020-01-14 08:52:40
函数在执行时,会在函数体内部自动生成一个this指针。谁 直接调用 产生这个this指针的函数 ,this就指向谁。 怎么理解指向呢,我认为指向就是等于。例如直接在js中输入下面的等式: console.log(this===window);//true 情况不同,this指向的对象也不同。例如: 1. 函数声明的情况 var bj=10; function add(){ var bj=20; console.log(this);//window console.log(this.bj);//10 console.log(bj);//20 console.log(this.bj+bj);//30 } add(); window.add(); (1) 执行了add()之后,此时的this指向的是window对象,为什么呢?因为这时候add是全局函数,是通过window 直接调用 的。所以下面我专门写了个window.add()就是为了说明,全局函数的this都是指向的window。 (2) 就像alert()自带的警告弹窗一样,window.alert()执行之后也是一样的效果。所以只要是 window点 这种调用方式都可以省略掉,因此警告弹窗可以直接使用alert()。 2. 函数表达式 var bj=10; var zjj=function(){ var bj=30;

Cancel multiple timeouts set to an ID

邮差的信 提交于 2020-01-14 01:38:28
问题 I'm not quite sure of the mechanics of this problem, but I'm trying to have a single setTimeout set to a variable ID that I can easily cancel using clearTimeout. But, if setTimeout gets triggered twice before clearTimeout, things go wacky. Example: http://www.w3schools.com/js/tryit.asp?filename=tryjs_settimeout2 Clicking "Try It" twice and then "Stop the Alert" twice, the function of set timeout still gets called. Likewise, I'm not sure why Try It would trigger the function twice considering

Update webpage to show progress while javascript is running in in a loop

给你一囗甜甜゛ 提交于 2020-01-13 19:40:47
问题 I have written javascript that takes 20-30 seconds to process and I want to show the progress by updating the progress bar on my webpage. I have used setTimeout in an attempt to allow webpage to be re-drawn. This is how my code looks like: function lengthyFun(...){ for(...){ var progress = ... document.getElementById('progress-bar').setAttribute('style',"width:{0}%".format(Math.ceil(progress))); var x = ... // Processing setTimeout(function(x) { return function() { ... }; }(x), 0); } } It

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);

setTimeout() and setting parameters

廉价感情. 提交于 2020-01-12 04:49:26
问题 I have some jQuery code that looks like this: $('.mainNav2 > li').mouseleave(function(){ var someNum = Math.random(); $(this).attr('id', someNum); var t = setTimeout("HideMenu(someNum)", 200); $('li.clicked').mouseenter(function() { clearTimeout(t); }); }); function HideMenu(id) { $('#'+id).removeClass('clicked'); } It's purpose is to hide a mega menu on mouse leave, but also takes into account accidental mouse leaves, by using a 300 millisecond setTimeout. If the user brings the mouse

setTimeout - callback argument must be a function

社会主义新天地 提交于 2020-01-11 11:24:23
问题 My code was working until i updated node.js to version 8.11.3 Now i always get error "callback argument must be a function" when trying to call a function with setTimeout. function testFunction(itemid, price) { var url = 'https://example.com'; var options = { method: 'get', url: url } request(options, function (err, res, body) { var response = JSON.parse(body); if(response.status == 'fail'){ setTimeout(testFunction(itemid, price), 100); } }) } 回答1: Callback argument for setTimeout must be a

setTimeout - callback argument must be a function

混江龙づ霸主 提交于 2020-01-11 11:24:09
问题 My code was working until i updated node.js to version 8.11.3 Now i always get error "callback argument must be a function" when trying to call a function with setTimeout. function testFunction(itemid, price) { var url = 'https://example.com'; var options = { method: 'get', url: url } request(options, function (err, res, body) { var response = JSON.parse(body); if(response.status == 'fail'){ setTimeout(testFunction(itemid, price), 100); } }) } 回答1: Callback argument for setTimeout must be a

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()

[翻译]JavaScript秘密花园 - Type Casting,undefined,eval,setTimeout,Auto Semicolon Insertion - 全部完成PDF打包下载

我只是一个虾纸丫 提交于 2020-01-11 06:30:06
JavaScript Garden - 原文 JavaScript Garden - 中文翻译 PDF打包下载 类型转换 JavaScript 是 弱类型 语言,所以会在 任何 可能的情况下应用 强制类型转换 。 // 下面的比较结果是:truenew Number(10) == 10; // Number.toString() 返回的字符串被再次转换为数字10 == '10'; // 字符串被转换为数字10 == '+10 '; // 同上10 == '010'; // 同上 isNaN(null) == false; // null 被转换为数字 0 // 0 当然不是一个 NaN(译者注:否定之否定)// 下面的比较结果是:false10 == 010;10 == '-10'; ES5 提示: 以 0 开头的数字字面值会被作为八进制数字解析。而在 ECMAScript 5 严格模式下,这个特性被 移除 了。 为了避免上面复杂的强制类型转换, 强烈 推荐使用 严格的等于操作符 。虽然这可以避免大部分的问题,但 JavaScript 的弱类型系统仍然会导致一些其它问题。 内置类型的构造函数(Constructors of built-in types) 内置类型(比如 Number 和 String )的构造函数在被调用时,使用或者不使用 new 的结果完全不同。 new