dsigg21445 2018-03-07 16:31
浏览 92

Javascript ajax仅在需要时刷新

I have a web application which is showing data that is recieved from an AJAX request to a PHP script running on the same Server. In my case it's the playback of a spotify player. Because I want that Information to be as up-to-date as possible (in case of a change of the song etc.), my current approach is to send a request every second. Is there a possibility to e.g. send a "data changed"-Trigger from the PHP Server to the Javascript, so that only then the AJAX-Request is triggered, in oder to reduce unwanted traffic for the client? Or is there another approach I'm missing with that this Problem can be solved more elegantly?

  • 写回答

1条回答 默认 最新

  • dongwuchu0314 2018-03-07 16:47
    关注

    Well since it's not a production product and just for personal use, what you could do is on:

    The JavaScript UI: make the call like you're doing now once. On completion, make the call again (success, error, timeout).

    The server side: Remember what the last song was, when you get a new song (comparing to previous) send that back though the open AJAX call.

    Basically it will act like a web socket and have a request open all the time to the server. This is not good design, but the quick and dirty to get your pet project going without having to restructure it to use web sockets.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!