Javascript在警报之前做一些事情 [英] Javascript do something before alert

查看:72
本文介绍了Javascript在警报之前做一些事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改颜色并在单击按钮时提醒消息.

I want to change the color and alert a message when a button is clicked.

function colorChange() {
  document.getElementById('word').style.color = "red";
  alert("color changed!");
}

我无法同时发出警报来更改颜色.

I cannot change the color at the same time with an alert.

推荐答案

在超时时间内包装警报

function colorChange() {
  document.getElementById('word').style.color = "red";
  setTimeout(function() {
  	alert("color changed!");
  },10)
}

<div id="word">
Word .....
</div>
<br />
<button onclick="colorChange()">
Change color
</button>

在警报触发之前,浏览器没有时间重新绘制,通过使用超时,您可以将警报延迟到重新绘制完成的下一个刻度为止.

The browser doesn't have time to repaint before the alert fires, by using a timeout you delay the alert until the next tick, when the repaint has completed.

这篇关于Javascript在警报之前做一些事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆