Can I use a JavaScript Alert box to show a countdown timer?
2015-10-30 10:45
901 查看
Can
I use a JavaScript Alert box to show a countdown timer?
up vote-1down votefavorite | I would like to display a count down timerin an alertbox. When the page loads, the alert should appears, counting down. When it reaches zero, the alert should disappear automatically, and the user can proceed to view the page. Can this be achieved using an alertbox? Example link here javascript alertdialog
| ||||||||||||||||||||
a comment |
2 Answers
activeoldestvotesup vote3down voteaccepted | It is not possible to use an alert dialog to do what you want. window.alertcan only display static text and must be actioned by the user before it will go away (i.e. they must click on "OK") However, what you can do instead is to put a "blanket" over the page which is essentially a <div>tag styled with CSS to full width and height, fixed position, and a z-index higher than everything on the page. Optionally you can also apply an opacity setting so the page is still visible. This blanket prevents users from interacting with page elements. On top of that you can put another <div>which has an even higher z-index in which you can place your timer element. You can update your timer with the setIntervalJavascript method, and upon reaching 0, it can remove the blanket and timer div and stop and clear the interval timer. Hope this helps!
| ||
add a comment |
up vote0down vote | You should use a divto overlay your page content until the countdown has ended. Append the new div to the page, and then change it's displayCSS property to noneto hide it after five seconds. HTMLand CSSbelow are just for example, they represent your own current content, obviously. It can be done like this - HTML Just for example's sake. <div id="examplecontent"></div> CSS #examplecontent { height: 100vh; width: 100vw; background-color: #000; } JavaScript window.onload = Show_Countdown; var counter = 5; function Show_Countdown() { var countDown_overlay = 'position:absolute;' + 'top:50%;' + 'left:50%;' + 'background-color:white;' + 'z-index:1002;' + 'overflow:auto;' + 'width:400px;' + 'text-align:center;' + 'height:400px;' + 'margin-left:-200px;' + 'margin-top:-200px'; $('body').append('<div id="overLay" style="' + countDown_overlay + '"><span id="time"></span></div>'); var timer = setInterval(function () { document.getElementById("time").innerHTML = counter; counter = (counter - 1); if (counter < 0) { clearInterval(timer); document.getElementById("overLay").style.display = 'none'; } }, 1000); } To go a bit further, you should disable/enable your background content dependant on whether the overlay is displayed. Working example here
| ||
add a comment |
Your Answer
相关文章推荐
- Javascript模块化编程2:AMD规范
- js关闭当前页面比较兼容的办法
- Javascript模块化编程 1 :模块的写法
- js基础知识集锦
- javascript 学习总结(六)RegExp对象
- 关于WEBAPI传输JSON的效率测试
- html中使用js进行登录md5加密提交并重定向新页面
- js中单选,反选,联动选择等等
- js对已经对象类型进行类型辨别
- javascript基本语法
- js的prototype属性
- cookie.js插件的README/cookie.js中文使用手册
- javascript学习心得
- 自己动手写的javascript前端等待控件
- Js获取当前日期时间及其它操作
- 处理将list 转换为json,js时间显示毫秒数问题
- JavaScript最佳实践
- JavaScript获取function所有参数名的方法
- 图文解说 JavaScript this 到底指向什么?
- Notepad 自动补全的插件 安装 (包括html,JavaScript)