settimeout

web api DOM_02

久未见 提交于 2019-12-16 22:52:39
创建元素的三种方式 document.write() 缺陷:如果是在页面加载完毕后,此时通过这种方式创建元素,那么页面上存在的所有的内容全部被干掉 document.write('新设置的内容<p>标签也可以生成</p>'); innerHTML var box = document.getElementById('box'); box.innerHTML = '新内容<p>新标签</p>'; document.createElement() var div = document.createElement('div'); document.body.appendChild(div); 性能问题 innerHTML方法由于会对字符串进行解析,需要避免在循环内多次使用。 可以借助字符串或数组的方式进行替换,再设置给innerHTML 优化后与document.createElement性能相近 - 动态创建列表,高亮显示 - 根据数据动态创建表格 模拟百度搜索文本框 元素相关得方法: 只创建一个元素 节点操作 、* 节点的属性:(可以使用标签–元素.出来,可以使用属性节点.出来,文本节点.点出来) nodeType:节点的类型:1----标签,2—属性,3—文本 nodeName:节点的名字:标签节点—大写的标签名字,属性节点—小写的属性名字,文本节点----#text

Passing functions to setTimeout in a loop: always the last value?

徘徊边缘 提交于 2019-12-16 22:52:10
问题 I'm trying to use setTimeout to execute an anonymous function that I pass information into and I'm having trouble. This (hard-coded version) would work just fine: setTimeout(function(){alert("hello");},1000); setTimeout(function(){alert("world");},2000); But I'm trying to take the hello and world from an array and pass them into the function without (a) using global variables, and (2) using eval. I know how I could do it using globals or eval, but how can I do it without. Here is what I'd

【javascript 进阶】异步调用

狂风中的少年 提交于 2019-12-16 20:56:20
前言 javascript的中的异步是很重要的概念,特别是ajax的提出,给整个web带来了很大的影响,今天就介绍下javascript的异步编程。 同步与异步 何为同步?何为异步呢? 同步:说白了就是程序一步一步从下向下执行,没有什么别的代码的跳动,就是按序执行,和在景区里女生上厕所是排队是一样的(每次女厕都是有好多人在排队)。可以看成是一个单线程问题。 异步:异步就是程序可以跳着执行,开始执行一段程序之后不用等返回结果就执行其他的代码,等结果返回之后在对结果进行处理,也就是可以在有限的时间内办好几件事情,提高效率,异步一般情况是多个线程问题。举个例子,还是上厕所的例子,A说需要5分钟搞定,B就可以5分钟之后过来,B可以在这5分钟干点别的事情。 所以说异步可以提高程序的执行效率,所以异步编程具有一定的好处,但是编写异步程序却不是那么容易的。 上面是我的理解,下面是摘自别人的文章: "同步模式"就是上一段的模式,后一个任务等待前一个任务结束,然后再执行,程序的执行顺序与任务的排列顺序是一致的、同步的;"异步模式"则完全不同,每一个任务有一个或多个回调函数(callback),前一个任务结束后,不是执行后一个任务,而是执行回调函数,后一个任务则是不等前一个任务结束就执行,所以程序的执行顺序与任务的排列顺序是不一致的、异步的。 浏览器机制 我们都知道了

promise, async和await

杀马特。学长 韩版系。学妹 提交于 2019-12-16 12:40:00
最开始实现异步的方法:回调函数 method1(function(err, result) { if (err) { throw err; } method2(function(err, result) { if (err) { throw err; } method3(function(err, result) { if (err) { throw err; } method4(function(err, result) { if (err) { throw err; } method5(result); }); }); }); }); 回调地狱一层一层嵌套多个回调函数,会使代码错综复杂,难以理解和调试。 promise promise是异步编程的一种解决方案,比传统的解决方案--回调函数和事件,更合理更强大。所谓promise,简单说就是一个容器,里面保存着某个未来才会结束的事件(通常是一个异步操作)的结果。从语法上说,promise是一个对象,可以获取异步操作的消息,提供统一的API,各种异步操作都可以用同样的方法进行处理。 特点:1、对象状态不受外界影响。Promise 对象代表一个异步操作,有三种状态:pending(进行中)、fulfilled(已成功)和 rejected(已失败)。只有异步操作的结果,可以决定当前是哪一种状态,任何其他操作都无法改变这个状态。 2

jQuery change background image to several divs at random intervals

家住魔仙堡 提交于 2019-12-14 03:57:07
问题 I have several divs displayed next to each other on my site, each with a different background image. I have created a script that changes the background images at a random time interval, However, the script changes all the images at the same time....I want each one to change randomly, totally independent of the other....I'd also like them to fade, but the timing is the key question here. Here is my script I'm working with. jQuery(window).load(function(){ var images = ['images/gallery

Javascript: Can't stop the setTimeout

拈花ヽ惹草 提交于 2019-12-14 03:57:04
问题 I'm working on a proxy server checker and have the following code to start the requests at intervals of roughly 5 seconds using the setTimeout function; function check() { var url = document.getElementById('url').value; var proxys = document.getElementById('proxys').value.replace(/\n/g,','); var proxys = proxys.split(","); for (proxy in proxys) { var proxytimeout = proxy*5000; t = setTimeout(doRequest, proxytimeout, url, proxys[proxy]); } } However I can't stop them once their started!

What determines the call order of deferred function using promises or setTimeout?

最后都变了- 提交于 2019-12-14 03:43:55
问题 Deferring the execution of functions, for example in custom event handling, is a common pattern in JavaScript (see, for example here). It used to be that using setTimeout(myFunc,0) was the only way to do this, however with promises there is now an alternative: Promise.resolve().then(myFunc) . I had assumed that these would pretty much do the same thing, but while working on a library which included custom events I thought I'd find out if there was a difference, so I dropped the following

setTimeout causes stack overflow

人走茶凉 提交于 2019-12-14 02:16:41
问题 I am have the following code. it causes a stack overflow exception. any idea what did I do wrong? var myApi = { rawData: null, initData: function() { // ajax call to get data and populate myApi.rawData, max 10 seconds }, waitForRawData: function(callback) { if(myApi.rawData === null || myApi.rawData.length ===0) { window.setTimeout(myApi.waitForRawData(callback),1000); // complain this line stack overflow }else{ callback(); } }, updateHtmlWithNewData: function() { // base on myApi.rawData

How do I make Ajax calls at intervals without overlap?

廉价感情. 提交于 2019-12-14 00:20:38
问题 I'm looking to setup a web page that samples data via AJAX calls from an embedded web-server. How would I set up the code so that one request doesn't overlap another? I should mention I have very little JavaScript experience and also a compelling reason not to use external libraries of any size bigger than maybe 10 or so kilobytes. 回答1: You may want to consider the option of relaunching your AJAX request ONLY after a successful response from the previous AJAX call. function autoUpdate() { var

Calling setTimeout() for all members in object - Never called for 1st member, and called for 2nd member. Why?

早过忘川 提交于 2019-12-13 23:27:39
问题 I have a 3D array (rather JS object), called outerArray in the SSCCE I am posting here in this question, and it contains inner arrays (rather objects), each of which contains several url objects, each of which contains a url field and an interval field. For a given inner array (rather object), the value of interval field for each url will be the same. That is how they have been grouped. My purpose is that , for each inner array, I send an AJAX request periodically at regular intervals, and