大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
var countdown = 60;
华坪网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站等网站项目制作,到程序开发,运营维护。创新互联自2013年起到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。
function settime(obj) {
if (countdown == 0) {
obj.html("获取验证码");
countdown = 60;
return;
} else {
obj.html(countdown + "s");
countdown--;
}
setTimeout(function () {
settime(obj)
}, 1000)
}
不添加控件,复制源码即可
Dim WithEvents timer1 As Timer
Dim counter As Integer
Private Sub Form_Load()
counter = 60
Set timer1 = Controls.Add("vb.timer", "Timer1", Me)
With timer1
.Interval = 1000
.Enabled = True
End With
Me.Font.Size = 16
End Sub
Private Sub timer1_timer()
Me.Cls
Me.Print counter
counter = counter - 1
If counter = 0 Then
timer1.Enabled = False
MsgBox "Time is up"
End If
End Sub
代码如下:
public class TimeCountDown {
public static void countDown(int seconds) {
System.err.println("倒计时" + seconds + "秒,倒计时开始:");
int i = seconds;
while (i 0) {
System.err.println(i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i--;
}
System.err.println(i);
System.err.println("倒计时结束");
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
countDown(60);// 倒计时1分钟
}
}
基于控制台的话很简单的,我跟你说一下大体思路吧,二话不说先来个for循环,然后输出倒计时的数字,程序睡一秒,在输出倒计时数字,如此循环,简单吧,下面看程序:
public static void main(String[] args) {
for(int i=10;i0;i--){
System.out.print(i+" ");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.err.print("爆炸");
}
其他基于网页的还是基于用户界面都可以使用这个思路的
import java.awt.BorderLayout;import java.awt.Container;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;public class TimerDemo extends JFrame implements ActionListener { private static final long serialVersionUID = 201306211111L; private JTextField screen = new JTextField("0"); private JButton start = new JButton("开始"); private JButton reset = new JButton("重置"); private JPanel panel = new JPanel(); private boolean isRunning; private int time; private int timeBetween; public TimerDemo(int timeBetween) { super("计时器"); this.timeBetween = timeBetween; try { init(); } catch (Exception e) { e.printStackTrace(); } } public TimerDemo() { super("计时器"); this.timeBetween = 100; try { init(); } catch (Exception e) { e.printStackTrace(); } } private void init() { panel.setLayout(new GridLayout()); panel.add(start); panel.add(reset); start.addActionListener(this); reset.addActionListener(this); screen.setFont(new Font("幼圆", Font.BOLD, 60)); screen.setHorizontalAlignment(JTextField.CENTER); screen.setEditable(false); Container c = getContentPane(); c.setLayout(new BorderLayout()); c.add(panel, BorderLayout.SOUTH); c.add(screen, BorderLayout.CENTER); this.setSize(200, 150); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(false); this.setLocationRelativeTo(null); this.setVisible(true); } public static void main(String[] args) { new TimerDemo(1);// 设定 1ms/次 // new TimerDemo(); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == start) { if (start.getText().equals("开始")) { start.setText("暂停"); isRunning = true; } else if (start.getText().equals("暂停")) { start.setText("开始"); isRunning = false; } } if (e.getSource() == reset) { start.setText("开始"); screen.setText("0"); isRunning = false; time = 0; } new Thread(new TimeZone()).start(); } class TimeZone implements Runnable { @Override public void run() { while (isRunning) { time++; if (time = Integer.MAX_VALUE) { screen.setText("ERROR"); JOptionPane.showMessageDialog(null, "ERROR"); isRunning = false; } screen.setText(String.valueOf(time)); try { Thread.sleep(timeBetween); } catch (Exception e) { e.printStackTrace(); } } } }}