第1关:定时器
设置执行一次的定时任务timerTask(),延迟为2000毫秒,任务的唯一标识赋值给变量timeId。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p onclick="handleTimer()">set timer then undo</p>
<script>
var timeId;
function timerTask() {
console.log("this is timer task");
}
function handleTimer() {
//请在此处编写代码
/********** Begin **********/
var timeId=window.setTimeout(timerTask,2000)
/********** End **********/
}
</script>
</body>
</html>
第2关:循环定时器
取消定时任务timerTask1(),要求使用本关介绍的方法。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p onclick="task1()">this is task onea</p>
<p onclick="task2()">this is task two</p>
<p onclick="removeTask1()">try to remove task one</p>
<script>
var timeId1;
var timeId2;
function timerTask1() {
console.log("timerTask1!");
}
function timerTask2() {
console.log("timerTask2!");
}
function task1() {
timeId1 = window.setInterval(timerTask1,2000);
}
function task2() {
timeId2 = window.setInterval(timerTask2,1500);
}
function removeTask1() {
//请在此处编写代码
/********** Begin **********/
window.clearInterval(timeId1);
window.clearInterval(timeId2);
/********** End **********/
}
</script>
</body>
</html>
第3关:location对象
第一步,获取location对象,赋值给变量loc;
第二步,利用loc打印出当前页面地址的主机名;
第三步,利用loc实现跳转到https://www.educoder.net/forums/categories/all?order=newest;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p onclick="openNew()">Click me to new page!</p>
<script>
function openNew() {
//请在此处编写代码
/********** Begin **********/
var loc=window.location;
console.log("hostname:"+loc.hostname);
window.location.href="https://www.educoder.net/forums/categories/all?order=newest"
/********** End **********/
}
</script>
</body>
</html>
第4关:对话框
第一步:弹出一个输入型对话框(输入框),用户的输入结果赋值给变量result,输入框提示语为your name,输入框默认值为Xiao Ming;
第二步:使用result判断用户的输入是否为null(不是字符串null,是表示空的null),如果是,在控制台打印name cannot be null
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p onclick="inputName()">Click to input name!</p>
<script>
function inputName() {
var result;
//请在此处编写代码
/********** Begin **********/
var result=window.prompt("your name","Xiao Ming");
if(result==mull){
console.log("name cannot be null")
}
/********** End **********/
}
</script>
</body>
</html>
第5关:窗口
打开一个窗口,它的文档的地址是Demo.html,name属性是窗口名字window_name,specs和replace不用设置。将打开的窗口赋值给变量myWindow
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p onclick="openNewWindow()">open new window</p>
<script>
var myWindow;
function openNewWindow() {
//请在此处编写代码
/********** Begin **********/
myWindow=window.open("Demo.html","window_name")
/********** End **********/
}
</script>
</body>
</html>