Coding Compendium
A free 100-page ebook collecting my projects and tutorials for Raspberry Pi, micro:bit, Scratch and Python. Simply join my newsletter to download it.
I mention this because I wonder whether this Javascript delay script is a similar dark force that shouldn't ever be needed if you're programming well. I'll share it with you anyway and let you worry about the beauty of your code.
This is the right way to make something happen after a delay in Javascript, using setTimeout() to trigger an alert after 1250 milliseconds.
Want to see it in action? No problem.
And here's the code that pulls that stunt:
That's the elegant way to handle delays, pauses and waiting in Javascript. Your teacher would be proud of you.
The routine above doesn't stop the Javascript routine from executing. It just sets up an event to happen later.
But sometimes you find yourself thinking: It would be so much easier if I could just get Javascript to wait for a bit here and then carry on as before.
It's a bit naughty, but I've written a script that does just that.
Note: This script is not a good example of how to use Javascript. It consumes a huge amount of CPU power just going round in circles for a predetermined amount of time. You really should be using the technique above in nearly all cases, but this script can be handy for prototyping.
Call the routine pausecomp(x) where x is the number of milliseconds you would like the computer to pause.
Many thanks to Michael Andrews who adapted my original code for seconds to make it work for milliseconds and Artur Kraft who sent me this code, which is more elegant than my original solution was. Thanks also to André Pirard for untwisting the variable declarations.
You're welcome to use this script on your site and to adapt it to suit.
Pavel Bakhilau has contacted me with a more concise version of the script:
For more Javascript examples, and an introduction to HTML and CSS too, see my new book Web Design in Easy Steps. It takes you through the whole process of building a website, from planning and content creation, through designing your layout and navigation, building your site, publishing it and promoting it.
The book includes extensive full colour examples and is written in the easy-to-follow style that the author has used in his contributions to magazines including Internet Magazine, Internet Works, and Business 2.0.
Find out more about the Web Design in Easy Steps book here.
You can buy the book here.
Photo: Stopwatch by Agê Barros at Unsplash.com
© Sean McManus. All rights reserved.
Visit www.sean.co.uk for free chapters from Sean's coding books (including Mission Python, Scratch Programming in Easy Steps and Coder Academy) and more!
A free 100-page ebook collecting my projects and tutorials for Raspberry Pi, micro:bit, Scratch and Python. Simply join my newsletter to download it.
Web Design in Easy Steps, now in its 7th Edition, shows you how to make effective websites that work on any device.
Power up your Microsoft Excel skills with this powerful pocket-sized book of tips that will save you time and help you learn more from your spreadsheets.
This book, now fully updated for Scratch 3, will take you from the basics of the Scratch language into the depths of its more advanced features. A great way to start programming.
Code a space adventure game in this Python programming book published by No Starch Press.
Discover how to make 3D games, create mazes, build a drum machine, make a game with cartoon animals and more!