alert()函数与console.log()函数区别
时间: 2023-11-05 12:37:28 浏览: 43
`alert()`函数和`console.log()`函数都是用于在浏览器中输出信息的函数,但它们有以下几个方面的区别:
1. 输出位置:`alert()`函数会在一个弹出框中输出信息,而`console.log()`函数会在浏览器的控制台输出信息。
2. 用途:`alert()`函数常用于在用户操作时给出提示信息,而`console.log()`函数常用于开发阶段的调试和输出日志信息。
3. 交互性:`alert()`函数会阻塞代码的执行,直到用户关闭弹出框,而`console.log()`函数不会阻塞代码的执行,可以直接输出信息。
4. 处理能力:`console.log()`函数可以输出各种类型的数据,包括字符串、数字、对象等,而`alert()`函数只能输出字符串类型的数据。
综上所述,`alert()`函数和`console.log()`函数虽然都可以用于输出信息,但它们的用途和输出位置不同,开发者需要根据具体情况选择合适的函数。
相关问题
console.log(alert('hello'))
console.log(alert('hello')) 的意思是在控制台输出一个警告框并弹出 "hello" 的提示信息。但是这段代码是错误的,因为 alert() 函数不会返回任何值,所以无法在 console.log() 中输出。应该将它们分开写,如下所示:
alert('hello');
console.log('hello');
function SendHttpRequest(cmd, respondfunc){ var xhr = new XMLHttpRequest(); xhr.timeout = 3000; xhr.ontimeout = function (event) { alert("请求超时!"); } var urlStr = 'http://127.0.0.1:5000/' + cmd; console.log(urlStr); xhr.open('GET', urlStr); xhr.setRequestHeader('content-type', 'application/json'); xhr.onreadystatechange = function () { // console.log(xhr.readyState) // console.log(xhr.status) if (xhr.readyState == 4 && xhr.status == 200) { console.log('ok'); if(respondfunc != null){ respondfunc(xhr.responseText); } } else if(xhr.status == 200){ console.log('waiting'); } else { console.log('err'); console.log(xhr.status); } } xhr.send(null); }
这段代码是一个 JavaScript 函数,用于向指定的 URL 发送 HTTP GET 请求,并在请求响应后执行指定的回调函数。该函数的参数包括:
- cmd:指定要发送的请求命令;
- respondfunc:指定请求响应后要执行的回调函数。
该函数使用 XMLHttpRequest 对象实现请求发送和响应处理。在发送请求前,设置了请求超时时间为 3 秒,并指定超时处理函数为弹出超时提示。在发送请求时,设置了请求头部的 content-type 为 application/json。在响应处理函数中,判断响应状态为 200 时,执行指定的回调函数,并将响应文本作为参数传递给回调函数。如果响应状态为 200 以外的值,则输出错误信息和状态码。