setinterval

In AngularJS, how to detect when user leaves template/page?

烈酒焚心 提交于 2019-11-28 15:46:24
问题 I am using the Javascript command: setInterval. I like to stop it when the user leaves the page. This code seems to work well: http://jsfiddle.net/PQz5k/ It detects when a user leaves a page. It executes Javascript code when a user clicks on a link to go to a different HTML page or URL, or if user reloads page. However, it does not work when I go from one AngularJS template to another. As an example, if I am at template1.html, I want the Javascript code to do something in Controller1.js when

定时器

拈花ヽ惹草 提交于 2019-11-28 14:49:10
一. setInterval()和setTimeout()   1. setTimeout():     (1)setTimeout()方法用于在指定的毫秒数后调用,即超时调用     (2)语法:setTimeout(fn,ms)     (3)使用clearTimeout()方法阻止函数的执行   2. setInterval():     (1)setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式,即间歇调用     (2)setInterval() 方法会不停地调用函数,直到 clearInterval() 被调用或窗口被关闭   3. 两者的区别与联系:     参考链接如下:        https://www.jianshu.com/p/fc9a08ca2c92        https://www.jianshu.com/p/aa72011e189f?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation        http://qingbob.com/difference-between-settimeout-setinterval/   4. 鉴于setInterval()的弊端,推荐使用setTimeout(

setTimeout/setInterval 1000ms lag in background tabs (Chrome and Firefox)

扶醉桌前 提交于 2019-11-28 11:30:16
When events are queued with setTimeout/setInterval, and the user is viewing a separate tab, Chrome and Firefox enforce a minimum 1000ms lag before the event is executed. This article details the behaviour. This has been discussed on StackOverflow previously, but the questions and answers only applied to animations. Obviously, an animation can just be forced to update to the latest state when a user re-enters the tab. But the solution does not work for sequenced audio. I have Web Audio API playing several audio files in sequence, and setTimeout is used to countdown to when the next audio file

JavaScript - jQuery interval

允我心安 提交于 2019-11-28 11:08:58
I am using JavaScript with jQuery. I have the following script to alert hi every 30 seconds. $(document).ready( function() { alert("hi"); setInterval(function() { alert("hi"); }, 30000); }); I want to alert hi when the page loads (when document / page gets completely loaded) and on every 30 seconds interval afterwards (like hi (0s) - hi (30s) - hi (60s).. etc). But my solution works on two instances. One on DOM ready and the other in a loop. Is there any way to do the same in a single instance? You can see my fiddle here . You could use setTimeout instead and have the callback reschedule

setInterval not working (firing only once) in Google Chrome extension

邮差的信 提交于 2019-11-28 10:57:15
Just as the title says: setInterval is only firing its callback once. manifest.json: { //... "content_scripts" : [{ "js" : ["code.js"], //... }], //... } code.js (example): setInterval(alert('only shown once'),2000); Why, and how I could fix it? The code works well outside of an extension (even in a bookmarklet). setInterval(function() { alert('only shown once') },2000); You need to pass a function reference like alert and not a return value alert() setInterval isn't working at all. The first argument should be a function , you are passing it the return value of alert() which isn't a function.

Javascript function in setInterval

喜夏-厌秋 提交于 2019-11-28 10:30:20
问题 I have the following code: var foo=5; var los= function (){ alert(foo);}; setInterval(los, 1000); which works correctly. If I change it to : var los= function (){ alert(foo);}; setInterval(los(), 1000); it only executes once with no errors in console. Can someone explain me why this happens when I include the parentesis after los in the setInterval function? 回答1: Keep in mind that in JavaScript a function is an object, passed around like any other variable. So this is a reference to the

JavaScript: How to do something every full hour?

痴心易碎 提交于 2019-11-28 09:43:51
问题 I want to execute some JS code every hour. But I can't use setInterval("javascript function",60*60*1000); because I want to do it every full hour, I mean in 1:00, in 2:00, in 3:00 and so on. I am thinking about something like var d; while(true) { d = new Date(); if ((d.getMinutes() == '00') && (d.getSeconds() == '00')){ // my code here } } but it's too slow and it doesn't work well. Thak you for any ideas 回答1: I would find out what time it is now, figure out how long it is until the next full

JQuery Mobile popup with history=false autocloses

杀马特。学长 韩版系。学妹 提交于 2019-11-28 08:57:41
问题 I'm trying to show a popup but the popup disappears automatically, without the history=false the popup stays visible but then on closing the popup the browser back action is triggered <div data-role="page" id="indexpage"> <div data-role="popup" data-history="false" id="appPopup">test popup</div> <script> $("#indexpage").on("pageshow", function () { $("#appPopup").popup("open"); }); </script> </div> Check what happens here: http://jsfiddle.net/francisdb/ThtfZ/ Any idea on how to fix this? 回答1:

如何解决setInterval 与页面切换问题

别来无恙 提交于 2019-11-28 08:50:05
setInterval 与页面切换问题 今天遇到的问题是我用好几个setInterval定时器来做模拟系统的动态表示,当打开页面时,程序能够正常运行,效果正常,但是当我切换到其他页面,过段时间再切回到系统的页面时,动画出现了错乱,不是按原来安排的运行。查了资料后,发现这是因为浏览器本着节省内存的性质,当切换到其他页面时,采油系统页面的定时器不运动,但是动画依然排列,当切换回来的时候,动画加速运动,出现错误,在轮播图之类的页面经常会发生这样的情况。 解决方案 我查了一部分资料,网上一般都是说如何在关闭页面的时候调用函数,比如用onbeforeunload和Onunload,但是我主要是在切换的时候出的问题,查了一上午,终于找到了监听页面切换的函数:如下所示: var hiddenProperty = 'hidden' in document ? 'hidden' : 'webkitHidden' in document ? 'webkitHidden' : 'mozHidden' in document ? 'mozHidden' : null ; var visibilityChangeEvent = hiddenProperty.replace( /hidden/i , 'visibilitychange' ); var onVisibilityChange = function

setInterval pauses in iphone/ipad (mobile Safari) during scrolling

跟風遠走 提交于 2019-11-28 08:44:25
I use the setInterval function in a website, and it works fine in IE, Chrome, Firefox and Safari. When i try it on ipad/iphone (safari mobile) i get problem: if i scroll the screen, the setInterval function pauses and it resumes only when i stop scrolling! is there a way to prevent the function from pausing? Thanks raina77ow I'm afraid no, there's no way to prevent such behaviour. There's a plenty of topics here in SO about this problem (more-o-less related), here's a particularly interesting one . Its summary is simple: iOS elastic scroll can't be tracked. There's no event listener for it +