settimeout

how to make ajax calls continuously for 5 seconds once

好久不见. 提交于 2019-12-23 03:15:19
问题 How to make ajax calls continuously for 5 seconds once. I have used setTimeout like below, setTimeout(function() { someFn(); }, 200); But it use more db connection. Can anyone tell me an effective way to call a function continuously for 5 seconds once. Function that i use inside setTimeout or setInterval will be used to update values to database.hence this will increase db connections. and result in lost db connection error.so i need something that will not increase db connections i am using

HTML5 - Fire event every few seconds while video is playing

对着背影说爱祢 提交于 2019-12-23 02:27:32
问题 I want to track user engagement for a video, by tracking how many users are dropping of at different time interval. For this I need to fire a tracking event, every 15 seconds while the video is playing. The playing event is triggered once. I need something which I can use throughout the life of the video. var _video = $('video')[0], timeoutID; $(_video).on('playing', function(event){ timeoutID = window.setTimeout(function() { console.log("video is playing"); }, 15000); }).on('ended', function

How do I remove the stack overflow from this casperjs code (using setTimeout)?

≯℡__Kan透↙ 提交于 2019-12-23 02:10:09
问题 The following sample resembles my actual code: function runCode() { casper.then(function(){ if (condition){ return; } }); .... code ..... .... code ..... casper.then(function(){ setTimeout(runCode(), 1000); }); } function startScript() { .... code .... .... code .... casper.then(function(){ runCode(); }); casper.then(function(){ setTimeout(startScript(),5000); }); } startScript(); This code is running on a vps and it seems to fill up all the 512 MB of RAM. It initially starts with around 50

JavaScript定时器

最后都变了- 提交于 2019-12-22 19:59:18
定时器 什么是定时器?作用? JS提供了一些原生方法来实现延时去执行某一段代码,下面简单介绍两种计时器。      setTimeOut: setTimeOut(code,millisec,lang) code:必选,要调用的函数后要执行的JavaScript代码串。 millisec:必选,在执行代码前需等待的毫秒数。 lang:可选,脚本语言可是是:JScript/VBScript/JavaScript setInterVal: setInterVal(code,millisec,lang) code:必选,要调用的函数后要执行的JavaScript代码串。 millisec:必选,在执行代码前需等待的毫秒数。 lang:可选,脚本语言可是是:JScript/VBScript/JavaScript 基本用法 setTimeOut: < script type = "text/javascript" > function myFunction ( ) { setTimeout ( function ( ) { alert ( "Hello" ) } , 3000 ) ; //3s后输出一次"Hello" } < / script > setInterVal: < script type = "text/javascript" > function myFunction ( ) {

Does “setTimeout(functon(){//do stuff},0)” cause a minuscule execution delay for it's contained function?

自古美人都是妖i 提交于 2019-12-22 16:07:13
问题 I set up an simple experiment out of curiosity: I dynamically generated a div with the class box before attaching a click listener to it with JQuery. I then created a duplicate of this, except this time, I added a timeout of zero before the box element is generated. As shown in the JSFiddle, using a timeout of zero results in the failure of the click's intended result event to be triggered. This script results in an alert on click. $(document).ready(function(){ //setTimeout(function(){ $("

Scoping rules for variables initialized in a for loop [duplicate]

会有一股神秘感。 提交于 2019-12-22 10:00:09
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Javascript closure inside loops - simple practical example I'm playing around with setTimeout in a project of mine in order to throttle the adding of elements to the DOM (so UI won't freeze during page loading). However, I've encountered something a bit puzzling to me. Given this code: for(var i = 0; i < 5; i++) { var j = i + 10; console.log("i is: " + i + " j is: " + j); setTimeout(function() { console.log("in

setTimeout not always working in Greasemonkey

你离开我真会死。 提交于 2019-12-22 08:07:41
问题 I've found a lot of similar problems but none equal and no right solution. This is a very strange issue. I have a simple Greasemonkey script to test the issue: // ==UserScript== // @name testdiddio // @namespace http://userscripts.org/users/useridnumber // @include https://www.google.it/ // @version 1 // ==/UserScript== function wait(){ console.info("wait"); setTimeout(wait,1000); } console.info("start"); wait(); This is the output from firebug: start wait wait wait wait The wait() function

Prevent JavaScript function from running twice (setTimeout)

梦想与她 提交于 2019-12-22 07:58:23
问题 I have this function that runs for several seconds with the use of setTimeout . This function is ran when a button is clicked. function complete() { var div = document.getElementById('log'); setTimeout(function(){ div.innerHTML = div.innerHTML + intro[Math.floor(Math.random() * intro.length)] + "<br>"; }, 500); setTimeout(function(){ div.innerHTML = div.innerHTML + second[Math.floor(Math.random() * second.length)] + "<br>"; }, 2560); setTimeout(function(){ div.innerHTML = div.innerHTML +

JS异步编程,回调函数与promise

夙愿已清 提交于 2019-12-22 04:33:10
  Promise 是异步编程的一种解决方案,比传统的解决方案——回调函数和事件——更合理和更强大。它由社区最早提出和实现,ES6 将其写进了语言标准,统一了用法,原生提供了 Promise 对象。   ES6之前,JavaScript中异步编程分为3类:DOM事件(如onclick)、网络请求(如ajax)、定时器(setTimeout/setInterval)。他们均使用回调函数来进行异步调用。当回调函数中嵌套了回调函数,甚至是多层回调时,编码就不够直观了。而使用Promise就能通过同步的编码方式实现异步调用。 1.多层回调:使用setTimeout()函数执行3层嵌套的异步回调,编码不直观 1 function async(){ 2 setTimeout(function(){ //回调函数1 3 console.log(1); 4 setTimeout(function(){ //回调函数2 5 console.log(2); 6 setTimeout(function(){ //回调函数2 7 console.log(3); 8 },1000); 9 },1000); 10 },1000) 11 } 12 13 async(); 14 15 //调用结果:1s后打印1 2s后打印2 3s后打印3 2.promise:以同步顺序编码来执行3次异步回调 通过return

js-异步机制与同步机制

♀尐吖头ヾ 提交于 2019-12-22 04:32:48
Javascript的优势之一是其如何处理异步代码。异步代码会被放入一个事件队列,等到所有其他代码执行后才进行,而不会阻塞线程 1 理解异步代码: 1.1 JavaScript最基础的异步函数是setTimeout和setInterval。setTimeout会在一定时间后执行给定的函数。它接受一个回调函数作为第一参数和一个毫秒时间作为第二参数。 console.log(1); setTimeout(function() { console.log('a'); },1000); setTimeout(function() { console.log('b'); },1000); setTimeout(function() { console.log('c'); },1000); console.log(2); 正如预期,控制台先输出1、2,大约500毫秒后,再看到“a”、“b”、“c”。我用“大约”是因为setTimeout事实上是不可预知的。实际上,甚至 HTML5 规范都提到了这个问题: “这个API不能保证计时会如期准确地运行。由于CPU负载、其他任务等所导致的延迟是可以预料到的。” 1.2 Event Loop队列 有趣的是,直到在同一程序段中所有其余的代码执行结束后,超时才会发生。所以如果设置了超时,同时执行了需长时间运行的函数,那么在该函数执行完成之前