Meet the Timers: setTimeout, setInterval, and requestAnimationFrame

  Рет қаралды 13,581

KIRUPA

KIRUPA

Күн бұрын

Learn how to delay execution of your code to the specified (or right) time by using setTimeout, setInterval, and requestAnimationFrame

Пікірлер: 17
@sweeball
@sweeball 6 жыл бұрын
Excellent!
@truumann199
@truumann199 8 жыл бұрын
thank you.
@davideugene7911
@davideugene7911 6 жыл бұрын
Merci :)
@misterhtmlcss
@misterhtmlcss 7 жыл бұрын
@kirupa can I use a time delay to limit the loading of JS scripts until 3-4 seconds later? 90% of my page doesn't need JS to start being useful to the client/viewer. I'm just wondering if this is something Google will frown upon or if that's a great way to speed up your page load time while still delivering the slick JS+ experience with standard HTML/CSS. Thoughts? PS. I've even read somewhere that guys use JS to load CSS and JS related to a specific form factor e.g. phones get only phone related HTML/CSS/JS for that platform size.
@kirupa
@kirupa 7 жыл бұрын
You could use "async" or "defer" on your script tags to load JS after your content has loaded. I write about them here: www.kirupa.com/html5/running_your_code_at_the_right_time.htm I think "async" is good enough, and you won't need to use a timer to load JS at a later time :P Regarding your PS, you can totally do that if you want to REALLY optimize how your content loads. I think that is overkill haha.
@sumanth3036
@sumanth3036 4 жыл бұрын
I know ur blog. U are awesome
@rickyu1978
@rickyu1978 7 жыл бұрын
just bought your kindle book.. question do you have an update for es6?
@kirupa
@kirupa 7 жыл бұрын
rickyu1978 No plans yet, for a lot of ES6 goes beyond the basics the book focuses on :-)
@Gerald-iz7mv
@Gerald-iz7mv 7 жыл бұрын
Hi, could you explain what the difference between requestAnimationFrame and requestAnimFrame is? which option do you refer to use and why? here are two examples: ideone.com/AwAFlq why does requestAnimFrame require a "window.setTimeout(callback, 1000 / 60);" and requestAnimationFrame not? i got the first example from here: codereview.stackexchange.com/questions/60216/gameloop-with-requestanimationframe
@kirupa
@kirupa 7 жыл бұрын
Gerald - from your example, requestAnimFrame is just a custom function that wraps requestAnimationFrame in a way to provide support for older browsers. It provides vendor-prefixed versions for older browsers as well as the setTimeout approach for browsers EVEN OLDER than the ones that need a vendor-prefixed version :P I'd say that you should just use requestAnimationFrame without worrying about browser fallbacks. Today, requestAnimationFrame has nearly universal coverage: caniuse.com/#feat=requestanimationframe. You should be safe to use it directly without doing anything extra.
@Gerald-iz7mv
@Gerald-iz7mv 7 жыл бұрын
so the example (ideone.com/AwAFlq) in line 1 - line 45 is good? it does not use a "window.setTimeout(callback, 1000 / 60);" ... ok? how can i add cancelAnimationFrame in that example (requestAnimationFrame example)?
@kirupa
@kirupa 7 жыл бұрын
Gerald Stan The example here should help: www.kirupa.com/html5/animating_with_requestAnimationFrame.htm :P
@Gerald-iz7mv
@Gerald-iz7mv 7 жыл бұрын
Nice article. They use setTimeout to slow down the animation e.g. 10 frames / second instead 60fps... So I only need the following vendor specific code to support old browsers? (function() { var w=windows, raf="RequestAnimationFrame"; window.requestAnimationFrame = w['webkit'+raf] || w['moz'+raf] || w['o'+raf] || ['ms'+raf] ; }());
@kirupa
@kirupa 7 жыл бұрын
Yep! An even simpler version of that looks like this: var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
@muthukumaranmurugaiyan9451
@muthukumaranmurugaiyan9451 4 жыл бұрын
are you tamil
Variable Scope in JavaScript
12:26
KIRUPA
Рет қаралды 10 М.
DSPy Explained!
54:16
Connor Shorten
Рет қаралды 55 М.
A teacher captured the cutest moment at the nursery #shorts
00:33
Fabiosa Stories
Рет қаралды 54 МЛН
Survive 100 Days In Nuclear Bunker, Win $500,000
32:21
MrBeast
Рет қаралды 76 МЛН
路飞太过分了,自己游泳。#海贼王#路飞
00:28
路飞与唐舞桐
Рет қаралды 37 МЛН
DOM vs. Canvas
10:44
KIRUPA
Рет қаралды 25 М.
The Hidden World of requestAnimationFrame
9:22
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 31 М.
The Art of Code - Dylan Beattie
1:00:49
NDC Conferences
Рет қаралды 4,7 МЛН
JavaScript Higher Order Functions & Arrays
34:56
Traversy Media
Рет қаралды 981 М.
Functions in JavaScript
12:45
KIRUPA
Рет қаралды 9 М.
Can you solve for the angle?
10:56
MindYourDecisions
Рет қаралды 11 М.
SVG Analogue World Clock with HTML, CSS, and JavaScript
48:41
Coding in Public
Рет қаралды 7 М.
Klavye İle Trafik Işığını Yönetmek #shorts
0:18
Osman Kabadayı
Рет қаралды 8 МЛН
Look, this is the 97th generation of the phone?
0:13
Edcers
Рет қаралды 8 МЛН
Better Than Smart Phones☠️🤯 | #trollface
0:11
Not Sanu Moments
Рет қаралды 17 МЛН
My iPhone 15 pro max 😱🫣😂
0:21
Nadir Show
Рет қаралды 1,5 МЛН
Rate This Smartphone Cooler Set-up ⭐
0:10
Shakeuptech
Рет қаралды 6 МЛН