Yesterday, I discussed a JS issue with Blade, that I have to set up an automatic photo-switch effect on an existing page. I don’t want to change anything except adding a piece of javascript for doing that job because this page is already messy up by another engineer. Blade suggested I should use setTimeout for this effect.
* Case Study
The basic case is something like below(there are some other effects based on the structure and style, so don’t affect others):
会议城市:海口
时间:8月26日 14:00-18:00
地点:海口鑫源温泉大酒店
Now the task is to write a js to roll the cities and regarding images, how to make it happen?
* Solution with setTimeout
Actually, you can figure out this effect with setInterval as well. The difference between setTimeout and setInterval is how many times you want to run the function which is set as their parameters. setTimeout will execute the function only once when time is out. But setInterval execute the function repeatedly in certain interval which is set as a parameter.
Every time you should remember to clear the Timeout or Interval after finish your job through clearTimeout and clearInterval.
I have linked the regarding references for the four methods about, you should read the document and related samples to know more details.
More Resources: 如何在jQuery中使用 setInterval,setTimeout jQuery:Tab切换功能的集合