Create a timer which will auto restart after every button click with the same information visible to all the users who are currently using the site

问题: I am planning to develop a website which works completely based on a timer. This is basically for a discount sales. Here I might have a number of users participating at the...

问题:

I am planning to develop a website which works completely based on a timer. This is basically for a discount sales. Here I might have a number of users participating at the same time where the timer is initialized to zero , and each time users click the discount button, the timer starts from 11 sec, counting down to zero. And if any users click the discount button in between, then again the timer needs to starts from 11 sec. And it will continue until when no users click the discount button. Here I am able to restart the timer, but its working is not synchronized in all browsers. Means, the timer that one users see in his browser is not same to the one another user see in his browser, that too for a same product, which all users are playing for. The concept is more of that of bidding only. Can anyone please help me with this?


回答1:

Your site needs to not only to reset the counter when the button is clicked, but also check the counter during the countdown. So basically on a timer on the client side you'll need to make an AJAX call back to update the counter.

So on the client side: - Start the counter when the page is loaded with the current value. - React to a click of the Discount button by sending a message to the server with with an AJAX call or by reloading the page. Likely AJAX as you need it anyway for the next item. - Check the server regularly for updates. Given we only have 11s, you are going to have to decide on timing. Every 3-4s might be best. Make an AJAX call back to get an update.

On the server side: - Send out pages to clients with the current counter value to start with. - React to a press of a Discount button by resetting the counter. - React to an update request with a quick return of the state of the counter.

I think all of those steps are required to get this to work. How you do them exactly is up to you, especially seeing as you've provided no code or anything.

  • 发表于 2019-07-05 02:55
  • 阅读 ( 149 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除